redux-saga-addons
v0.1.0
Published
Focused high-level helpers for Redux Saga.
Readme
redux-saga-addons
Focused high-level helpers for Redux Saga.
The first helpers apply takeLatest and takeLeading independently to each key
selected from an action or channel item. Work for different keys remains
concurrent.
Installation
npm install redux-saga-addons redux-sagaOther package managers:
yarn add redux-saga-addons redux-saga
pnpm add redux-saga-addons redux-saga
bun add redux-saga-addons redux-sagaUsage
Latest task per key
import { call } from "redux-saga/effects";
import { takeLatestPerKey } from "redux-saga-addons";
function* fetchUser(action) {
const user = yield call(api.fetchUser, action.payload.id);
// ...
}
function* rootSaga() {
yield takeLatestPerKey("FETCH_USER", fetchUser, (action) => action.payload.id);
}If another FETCH_USER action arrives for the same ID, its previous worker is
cancelled. Workers for other IDs continue running.
Leading task per key
import { takeLeadingPerKey } from "redux-saga-addons";
function* rootSaga() {
yield takeLeadingPerKey("SAVE_USER", saveUser, (action) => action.payload.id);
}While a worker is running, another matching item with the same key is ignored. Items with other keys still start their own workers.
API
takeLatestPerKey(patternOrChannel, worker, keySelector, ...args)
Returns a Redux Saga fork effect. The worker receives ...args followed by the
matching action or channel item. A new item cancels the running worker selected
by the same key before starting its replacement.
takeLeadingPerKey(patternOrChannel, worker, keySelector, ...args)
Returns a Redux Saga fork effect. The worker receives ...args followed by the
matching action or channel item. A new item is ignored when a worker selected by
the same key is already running.
Key selectors
keySelector must be synchronous and side-effect-free. It may return any
non-thenable value; keys use JavaScript Map identity, so objects, symbols,
strings, and numbers are all supported without string coercion.
An asynchronous selector is rejected. Resolve anything needed to choose the key before dispatching the action or writing the channel item.
Task lifecycle
- Cancelling the watcher cancels its active workers.
- Unhandled worker or selector errors propagate through normal attached Redux Saga task semantics.
- Dispatching
END, or closing an input channel, stops the watcher according to normal Redux Saga behavior. - Completed and cancelled tasks are removed from internal bookkeeping.
Runtime requirements
- Redux Saga 1.5 or newer
- An ESM-compatible runtime or bundler with native generator support
This package ships its source directly. It does not include generator runtime polyfills, transpilation helpers, or other compatibility shims. Applications are responsible for any compatibility support required by their target runtimes.
TypeScript declarations are included with the package.
Development
Run runtime and declaration tests:
bun run checkInspect the package contents before publishing:
bun pm pack --dry-runBackground
The per-key helpers grew out of redux-saga/redux-saga#1744 and the earlier discussion in redux-saga/redux-saga#694. The original approach was proposed by @Andarist.
