Created
April 14, 2017 10:03
-
-
Save mori-dev/22cfd5e1d62ccaf4012b141185fde37c to your computer and use it in GitHub Desktop.
redux アプリで定期実行処理を書くには / saga
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
| // @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