Skip to content

Instantly share code, notes, and snippets.

@PaulLaux
Last active September 4, 2018 06:30
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 PaulLaux/fb9a88ada76ca4df48fcfa4f66de6068 to your computer and use it in GitHub Desktop.
Save PaulLaux/fb9a88ada76ca4df48fcfa4f66de6068 to your computer and use it in GitHub Desktop.
Polling pattern using redux-saga
export default function* defaultSaga() {
// ...
// The polling will start only after the first CHECK_BALANCES ends
yield [
fork(watchPollData),
takeLatest(CHECK_BALANCES, checkAllBalances),
];
}
// Start Polling when first call to checkAllBalances succeded or fails
// Wait for successful response or fail, then fire another request
// Cancel polling on STOP_POLL_BALANCES
function* watchPollData() {
while (true) {
yield take([CHECK_BALANCES_SUCCESS, CHECK_BALANCES_ERROR]);
yield race([
call(pollData),
take(STOP_POLL_BALANCES),
]);
}
}
// Fetch data every timeBetweenCheckBalances seconds
function* pollData() {
yield call(delay, timeBetweenCheckBalances);
yield put(checkBalances());
}
// Utility function for delay effects
function delay(millisec) {
return new Promise(resolve => setTimeout(() => resolve(true), millisec));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment