How do I create a leading debounce with redux-saga

陌路散爱 提交于 2020-01-03 15:52:07

问题


Is there a way to do a leading debounce?

The example on the recipes only shows a trailing debounce. So below is trailing debounce example where we delay the logic fro 500ms:

import { call, cancel, fork, take, delay } from 'redux-saga/effects'

function* handleInput(input) {
  // debounce by 500ms
  yield delay(500)
  ...
}

function* watchInput() {
  let task
  while (true) {
    const { input } = yield take('INPUT_CHANGED')
    if (task) {
      yield cancel(task)
    }
    task = yield fork(handleInput, input)
  }
}

where as i'd like to execute logic on the first call that cancel any subsequent calls until 500ms has ended.

Edit

This can be done by using takeLeading then delaying the saga at the end by however long you want to debounce for.


回答1:


This can be done by using takeLeading then delaying the saga at the end by however long you want to debounce for.



来源:https://stackoverflow.com/questions/50489167/how-do-i-create-a-leading-debounce-with-redux-saga

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!