Created
March 1, 2024 11:15
-
-
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
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
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