Skip to content

Instantly share code, notes, and snippets.

@chris-kobrzak
Created March 1, 2024 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-kobrzak/5d65215f179ccc3a41c54438751f6ecd to your computer and use it in GitHub Desktop.
Save chris-kobrzak/5d65215f179ccc3a41c54438751f6ecd to your computer and use it in GitHub Desktop.
Generic Redux Saga task that waits for an event and triggers another timeout event if the former never gets dispatched
function* takeWithTimerTask(
eventName: AnyEvent['type'],
timeoutEventName: AnyEvent['type'],
timeout: number
) {
const timerTask = yield* fork(
fireEventAfterDelayTask,
timeoutEventName,
timeout
)
yield* take(eventName)
yield* cancel(timerTask)
}
function* fireEventAfterDelayTask(
eventType: AnyEvent['type'],
timeout: number
) {
while (true) {
yield delay(timeout)
yield put({ type: eventType })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment