@augmentcode/ag-redux-toolkit
core/channel-effects
sub-skill>-
Channel Effects — generic EventChannel consumers
Use native redux-saga takeEvery(channel, worker) for every-event EventChannel<T> consumption and takeLatest(channel, worker) when each new event should cancel the previous worker.
Source: redux-saga channel watcher effects, ../SKILL.md §5 (Channel Effects).
Imports
import type { EventChannel } from "redux-saga";
import { takeEvery, takeLatest } from "redux-saga/effects";Native channel watchers
takeEvery(channel, worker);
takeLatest(channel, worker);For every-event handling, call redux-saga's takeEvery(channel, worker) directly. For latest-only handling, call redux-saga's takeLatest(channel, worker) directly. Native watcher effects do not own app-created channel resources; wrap usage in try/finally and close the channel yourself.
Core Patterns
1. Fork a new worker for every event
// createAppEventChannel is app-provided and returns EventChannel<MyEvent>
const channel = createAppEventChannel<MyEvent>("my:event");
try {
yield* takeEvery(channel, function* (data) {
yield* put(handleEvent(data));
});
} finally {
channel.close();
}Every event spawns a new worker concurrently — use when events are independent. Keep ownership of app-created channels explicit and close them in finally.
2. Cancel the previous worker for each new event
// createAppEventChannel is app-provided and returns EventChannel<MyEvent>
const channel = createAppEventChannel<MyEvent>("my:event");
try {
yield* takeLatest(channel, function* (data) {
yield* call(expensiveOperation, data);
});
} finally {
channel.close();
}Use native redux-saga takeLatest(channel, worker) when only the latest event matters (e.g., streaming progress, latest search). Keep ownership of app-created channels explicit and close them in finally.
3. Replace a hand-rolled while (true) + take(channel) loop
// Before — easy to forget channel.close() on error
// createAppEventChannel is app-provided and returns EventChannel<MyEvent>
const channel = createAppEventChannel<MyEvent>("my:event");
try {
while (true) {
const data = yield* take(channel);
yield* call(handleEvent, data);
}
} finally {
channel.close();
}
// After
const channel = createAppEventChannel<MyEvent>("my:event");
try {
yield* takeEvery(channel, function* (data) {
yield* call(handleEvent, data);
});
} finally {
channel.close();
}Common Mistakes
❌ Hand-rolling the take-loop + finally close
Mechanism: Manual loops repeatedly forget the try/finally and leak the underlying subscription on cancellation. Prefer redux-saga takeEvery(channel, worker) for every-event handling, while keeping channel cleanup explicit.
// WRONG
const ch = createAppEventChannel<Evt>("e");
while (true) {
const data = yield* take(ch);
yield* call(worker, data);
}// CORRECT
const ch = createAppEventChannel<Evt>("e");
try {
yield* takeEvery(ch, worker);
} finally {
ch.close();
}Source: redux-saga channel watcher effects. Priority: HIGH.
❌ Using channel-effects where selector-channels fit
Mechanism: For selector-derived changes, takeEveryFromSelector / takeLatestFromSelector handle subscription and args-tuple keying automatically; wrapping a selector in a generic EventChannel throws those guarantees away.
// WRONG
const ch = yield* createChannelFromSelector(selectItem, [id]);
yield* takeLatest(ch, worker);// CORRECT
yield* takeLatestFromSelector(selectItem, [id], worker);Source context: package-internal store-utility saga implementation. Public selector-channel helpers are available from @augmentcode/ag-redux-toolkit/saga. Priority: MEDIUM.
See also
- core/selector-channels — for selector-derived channels (the right tool for state changes).
- core/sagas — core saga patterns, takeEvery / takeLatest for actions.