Skip to content

Instantly share code, notes, and snippets.

@nacmartin
Last active September 24, 2019 15:45
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 nacmartin/f0ac84e939bf5b4346007666ca017aed to your computer and use it in GitHub Desktop.
Save nacmartin/f0ac84e939bf5b4346007666ca017aed to your computer and use it in GitHub Desktop.
Before all section
import * as effects from "redux-saga/effects";
import * as constants from "../actions/constants";
function* processTaskTimeConsuming() {
yield effects.delay(1000 + 1000 * Math.random());
}
function* doProcessTask(name) {
try {
yield effects.put({ type: constants.TASK_PROCESS_START, name });
yield effects.call(processTaskTimeConsuming);
yield effects.put({ type: constants.TASK_PROCESS_DONE, name });
} catch (error) {
yield effects.put({ type: constants.TASK_PROCESS_ERROR, name });
} finally {
if (yield effects.cancelled()) {
yield effects.put({ type: constants.TASK_PROCESS_RESET, name });
}
}
}
function* processTask(action) {
yield effects.call(doProcessTask, action.name);
}
function* processTaskTimed(action) {
yield effects.race({
response: effects.call(doProcessTask, action.name),
cancelled: effects.take(constants.TASK_PROCESS_CANCEL_ALL)
});
}
export function* rootSaga() {
yield effects.takeEvery(constants.TASK_PROCESS, processTaskTimed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment