Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
redux アプリで定期実行処理を書くには / saga
// @flow
import 'babel-polyfill';
import { fork, take, call, put, cancel, cancelled } from 'redux-saga/effects';
import { delay } from 'redux-saga';
import { exec, end } from '../../actions/periodic_inspections/some_check';
import * as ActionTypes from '../../actions/action_types';
// test 用に export
export const intervalMs = Number(APP.INTERVAL_SAMPLE_CHECK);
// test 用に export
export function* worker(): Generator<any, any, any> {
try {
while (true) {
yield put(exec());
yield call(delay, intervalMs);
}
} finally {
if (yield cancelled()) {
yield put(end());
}
}
}
export function* watchSampleTrigger(): Generator<any, any, any> {
while (yield take(ActionTypes.CHECK_SAMPLE_TRIGGER)) {
const workerTask = yield fork(worker);
yield take([ActionTypes.CHECK_SAMPLE_STOP, ActionTypes.LOGOUT_OK]);
yield cancel(workerTask);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment