Skip to content

Instantly share code, notes, and snippets.

@krutoo
Last active June 25, 2020 06:38
Show Gist options
  • Save krutoo/b2898428b9bf93e25d6c917f01faf697 to your computer and use it in GitHub Desktop.
Save krutoo/b2898428b9bf93e25d6c917f01faf697 to your computer and use it in GitHub Desktop.
takeChain redux-saga custom effect
export const takeChain (timeout, types, task, ...args) => fork(function * () {
while (true) {
const BREAK_TYPE = `BREAK_${performance.now()}`;
const collectedActions = [yield take(types)];
yield fork(putDelayed, timeout, BREAK_TYPE);
while (true) {
const { action, canceled } = yield race({
action: take(types),
canceled: take(BREAK_TYPE),
});
action && collectedActions.push(action);
if (canceled || types.every(type => collectedActions.some(typeEq(type)))) {
yield fork(task, ...args, collectedActions);
break;
}
}
}
});
function * putDelayed (timeout, type) {
yield delay(timeout);
yield put({ type });
}
const typeEq = type => action => action.type === type;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment