Last active
June 25, 2020 06:38
-
-
Save krutoo/b2898428b9bf93e25d6c917f01faf697 to your computer and use it in GitHub Desktop.
takeChain redux-saga custom effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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