I am polling api with delay and cancellation using race condition.
What i want to achieve is when my page renders, i want to start polling for scores and on some action i will call same polling (same api) with some other query params so that i just want to update the same polling with new params.
for that i have done something like this.
export function* pollScoreSnippets() {
while (true) {
try {
const { data } = yield call(() => request(apis.GET_SCORE_API));
yield put({
type: types.DASHBOARD_DATA_FETCHED,
payload: {
type: ['scores'],
data: {
scores: { values: data.data },
},
},
});
yield call(delay, SCORE_SNIPPET_POLLING_DELAY);
} catch (err) {
yield put({
type: types.DASHBOARD_DATA_FETCHING_ERROR,
payload: {
error: err.response.data,
},
});
yield call(delay, SCORE_SNIPPET_POLLING_DELAY + 10);
}
}
}
export function* watchPollSaga() {
while (true) {
// console.log('watching');
yield take(types.POLL_SCORE_SNIPPETS);
yield race([call(dashGenerators.pollScoreSnippets), take(types.STOP_POLLING_SCORE_SNIPPETS)]);
}
}
That is working for me but with this approach i have to call cancel action then again same action for restarting polling again.
Is there any way that if i will call same action and either it will update the current polling request or cancel or restart with new polling request
something kind of:
export function* watchPollSaga() {
while (true) {
// console.log('watching');
yield take(types.POLL_SCORE_SNIPPETS);
yield race([call(dashGenerators.pollScoreSnippets), take(types.POLL_SCORE_SNIPPETS)]);
}
}