Skip to content

Instantly share code, notes, and snippets.

@joaojusto
Created April 21, 2022 09:22
Show Gist options
  • Select an option

  • Save joaojusto/d98dffce2dc2bcc46d01b04ce89f85e2 to your computer and use it in GitHub Desktop.

Select an option

Save joaojusto/d98dffce2dc2bcc46d01b04ce89f85e2 to your computer and use it in GitHub Desktop.
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