Skip to content

Commit

Permalink
Refactor to make it smart
Browse files Browse the repository at this point in the history
  • Loading branch information
kuy committed May 11, 2016
1 parent f7c1e01 commit ad65b62
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions autocomplete/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ function* runRequestSuggest(text) {
}
}

function forkLater(task, ...args) {
return fork(function* () {
yield call(delay, 1000);
yield fork(task, ...args);
});
function createLazily(msec = 1000) {
let ongoing;
return function* (task, ...args) {
if (ongoing && ongoing.isRunning()) {
ongoing.cancel();
}
ongoing = yield fork(function* () {
yield call(delay, msec);
yield fork(task, ...args);
});
}
}

function* handleRequestSuggest() {
let task;
const lazily = createLazily();
while (true) {
const { payload } = yield take(REQUEST_SUGGEST);
if (task && task.isRunning()) {
task.cancel();
}
task = yield forkLater(runRequestSuggest, payload);
yield fork(lazily, runRequestSuggest, payload);
}
}

Expand Down

0 comments on commit ad65b62

Please sign in to comment.