Skip to content

Instantly share code, notes, and snippets.

@mori-dev
Created April 14, 2017 10:03
Show Gist options
  • Select an option

  • Save mori-dev/22cfd5e1d62ccaf4012b141185fde37c to your computer and use it in GitHub Desktop.

Select an option

Save mori-dev/22cfd5e1d62ccaf4012b141185fde37c to your computer and use it in GitHub Desktop.
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