Last active
September 4, 2018 06:30
-
-
Save PaulLaux/fb9a88ada76ca4df48fcfa4f66de6068 to your computer and use it in GitHub Desktop.
Polling pattern using redux-saga
This file contains 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
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