Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Last active April 30, 2018 04:26
Show Gist options
  • Save Calvin-Huang/698cbb954d714a41c1726d0cee1be629 to your computer and use it in GitHub Desktop.
Save Calvin-Huang/698cbb954d714a41c1726d0cee1be629 to your computer and use it in GitHub Desktop.
// Debouncing
function* handleInput(input) {
// debounce by 500ms
yield call(delay, 500)
...
}
function* watchInput() {
let task
while (true) {
const { input } = yield take('INPUT_CHANGED')
if (task) {
yield cancel(task)
}
task = yield fork(handleInput, input)
}
}
// Debouncing written by takeLatest
function* handleInput({ input }) {
// debounce by 500ms
yield call(delay, 500)
...
}
function* watchInput() {
// will cancel current running handleInput task
yield takeLatest('INPUT_CHANGED', handleInput);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment