Created
April 21, 2022 09:22
-
-
Save joaojusto/d98dffce2dc2bcc46d01b04ce89f85e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { PayloadAction } from '@reduxjs/toolkit'; | |
| import { REHYDRATE } from 'redux-persist'; | |
| import { delay, fork, put, select, take, takeLatest } from 'typed-redux-saga'; | |
| import { executeStoryAction } from '~mission/storyActionHandlers'; | |
| import { NextStoryActionType, selectDialogue, setNextStoryAction } from '~redux/dialogue-slice'; | |
| function* handleSetNextStoryAction(action: PayloadAction<NextStoryActionType>) { | |
| const nextStoryAction = action.payload; | |
| if (nextStoryAction) { | |
| const { trigger, element } = nextStoryAction; | |
| const now = Date.now(); | |
| if (trigger > now) { | |
| yield delay(trigger - now); | |
| } | |
| yield put(executeStoryAction(element) as any); | |
| yield put(setNextStoryAction(undefined)); | |
| } | |
| } | |
| function* resumeNextStoryAction() { | |
| yield* take(REHYDRATE); | |
| const { nextStoryAction } = yield* select(selectDialogue); | |
| if (nextStoryAction) { | |
| yield put(setNextStoryAction(nextStoryAction)); | |
| } | |
| } | |
| export default function* nextStoryActionSaga() { | |
| yield* takeLatest(setNextStoryAction, handleSetNextStoryAction); | |
| yield* fork(resumeNextStoryAction); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment