Spawns a saga on an action dispatched to the Store that matches pattern.
Saga will be called after it stops taking pattern actions for ms
milliseconds. Purpose of this is to prevent calling saga until the actions
are settled off.
In the following example, we create a basic task fetchAutocomplete. We use
debounce to delay calling fetchAutocomplete saga until we stop receive
any FETCH_AUTOCOMPLETE events for at least 1000 ms.
import { call, put, debounce } from redux-saga/effects
function* fetchAutocomplete(action) { const autocompleteProposals = yield call(Api.fetchAutocomplete, action.text) yield put({type: 'FETCHED_AUTOCOMPLETE_PROPOSALS', proposals: autocompleteProposals}) }
function* debounceAutocomplete() { yield debounce(1000, 'FETCH_AUTOCOMPLETE', fetchAutocomplete) }
debounce is a high-level API built using take, delay and fork. Here
is how the helper could be implemented using the low-level Effects
const debounce = (ms, pattern, task, ...args) => fork(function*() { while (true) { let action = yield take(pattern)
while (true) {
const { debounced, _action } = yield race({
debounced: delay(ms),
_action: take(pattern)
})
if (debounced) {
yield fork(worker, ...args, action)
break
}
action = _action
}
}
})
defines how many milliseconds should elapse since the last time
pattern action was fired to call the saga
for more information see docs for take(pattern)
arguments to be passed to the started task. debounce will add
the incoming action to the argument list (i.e. the action will be the last
argument provided to saga)
Spawns a saga on an action dispatched to the Store that matches pattern.
Saga will be called after it stops taking pattern actions for ms
milliseconds. Purpose of this is to prevent calling saga until the actions
are settled off.
In the following example, we create a basic task fetchAutocomplete. We use
debounce to delay calling fetchAutocomplete saga until we stop receive
any FETCH_AUTOCOMPLETE events for at least 1000 ms.
import { call, put, debounce } from redux-saga/effects
function* fetchAutocomplete(action) { const autocompleteProposals = yield call(Api.fetchAutocomplete, action.text) yield put({type: 'FETCHED_AUTOCOMPLETE_PROPOSALS', proposals: autocompleteProposals}) }
function* debounceAutocomplete() { yield debounce(1000, 'FETCH_AUTOCOMPLETE', fetchAutocomplete) }
debounce is a high-level API built using take, delay and fork. Here
is how the helper could be implemented using the low-level Effects
const debounce = (ms, pattern, task, ...args) => fork(function*() { while (true) { let action = yield take(pattern)
while (true) {
const { debounced, _action } = yield race({
debounced: delay(ms),
_action: take(pattern)
})
if (debounced) {
yield fork(worker, ...args, action)
break
}
action = _action
}
}
})
Spawns a saga on an action dispatched to the Store that matches pattern.
Saga will be called after it stops taking pattern actions for ms
milliseconds. Purpose of this is to prevent calling saga until the actions
are settled off.
In the following example, we create a basic task fetchAutocomplete. We use
debounce to delay calling fetchAutocomplete saga until we stop receive
any FETCH_AUTOCOMPLETE events for at least 1000 ms.
import { call, put, debounce } from redux-saga/effects
function* fetchAutocomplete(action) { const autocompleteProposals = yield call(Api.fetchAutocomplete, action.text) yield put({type: 'FETCHED_AUTOCOMPLETE_PROPOSALS', proposals: autocompleteProposals}) }
function* debounceAutocomplete() { yield debounce(1000, 'FETCH_AUTOCOMPLETE', fetchAutocomplete) }
debounce is a high-level API built using take, delay and fork. Here
is how the helper could be implemented using the low-level Effects
const debounce = (ms, pattern, task, ...args) => fork(function*() { while (true) { let action = yield take(pattern)
while (true) {
const { debounced, _action } = yield race({
debounced: delay(ms),
_action: take(pattern)
})
if (debounced) {
yield fork(worker, ...args, action)
break
}
action = _action
}
}
})
defines how many milliseconds should elapse since the last time
pattern action was fired to call the saga
for more information see docs for take(pattern)
arguments to be passed to the started task. debounce will add
the incoming action to the argument list (i.e. the action will be the last
argument provided to saga)
Spawns a saga on an action dispatched to the Store that matches pattern.
Saga will be called after it stops taking pattern actions for ms
milliseconds. Purpose of this is to prevent calling saga until the actions
are settled off.
In the following example, we create a basic task fetchAutocomplete. We use
debounce to delay calling fetchAutocomplete saga until we stop receive
any FETCH_AUTOCOMPLETE events for at least 1000 ms.
import { call, put, debounce } from redux-saga/effects
function* fetchAutocomplete(action) { const autocompleteProposals = yield call(Api.fetchAutocomplete, action.text) yield put({type: 'FETCHED_AUTOCOMPLETE_PROPOSALS', proposals: autocompleteProposals}) }
function* debounceAutocomplete() { yield debounce(1000, 'FETCH_AUTOCOMPLETE', fetchAutocomplete) }
debounce is a high-level API built using take, delay and fork. Here
is how the helper could be implemented using the low-level Effects
const debounce = (ms, pattern, task, ...args) => fork(function*() { while (true) { let action = yield take(pattern)
while (true) {
const { debounced, _action } = yield race({
debounced: delay(ms),
_action: take(pattern)
})
if (debounced) {
yield fork(worker, ...args, action)
break
}
action = _action
}
}
})
defines how many milliseconds should elapse since the last time
pattern action was fired to call the saga
arguments to be passed to the started task. debounce will add
the incoming action to the argument list (i.e. the action will be the last
argument provided to saga)
Spawns a
sagaon an action dispatched to the Store that matchespattern. Saga will be called after it stops takingpatternactions formsmilliseconds. Purpose of this is to prevent calling saga until the actions are settled off.Example
In the following example, we create a basic task
fetchAutocomplete. We usedebounceto delay callingfetchAutocompletesaga until we stop receive anyFETCH_AUTOCOMPLETEevents for at least1000ms.import { call, put, debounce } from
redux-saga/effectsfunction* fetchAutocomplete(action) { const autocompleteProposals = yield call(Api.fetchAutocomplete, action.text) yield put({type: 'FETCHED_AUTOCOMPLETE_PROPOSALS', proposals: autocompleteProposals}) }
function* debounceAutocomplete() { yield debounce(1000, 'FETCH_AUTOCOMPLETE', fetchAutocomplete) }
Notes
debounceis a high-level API built usingtake,delayandfork. Here is how the helper could be implemented using the low-level Effectsconst debounce = (ms, pattern, task, ...args) => fork(function*() { while (true) { let action = yield take(pattern)
})