Skip to content

Instantly share code, notes, and snippets.

@wldcordeiro
Created April 4, 2019 01:33
Show Gist options
  • Save wldcordeiro/d07e6ecbd9a2e01524c7675647d007b0 to your computer and use it in GitHub Desktop.
Save wldcordeiro/d07e6ecbd9a2e01524c7675647d007b0 to your computer and use it in GitHub Desktop.
Polling in Redux Thunk
function polling(tries) {
return async (dispatch, getState) => {
const result = await Promise.all([
loadIntegrations(), // calling fetch()
getAllEnabledCalendarEvents(), // calling fetch()
]);
if (Array.isArray(result)) {
if (
result[0].every((calendar) => calendar.name && calendar.id) &&
areDifferentBy(getState().time.integratedCalendars, result[0])
) {
dispatch(setPolling(false));
dispatch(setPollingInterval(null));
dispatch(
getIntegratedCalendarEvents(
(result[1] || []).map(formatIntegrationEvent)
)
);
dispatch(getIntegratedCalendars(result[0]));
}
}
if (tries >= 100) {
// There seems to be no way in Jest to mock the 100 tries
// and see these `dispatch()` calls in the `store.getActions();`
// return value.
dispatch(setPolling(false));
dispatch(setPollingInterval(null));
const msgKey = 'wfhomecalendar.outlook.synctimeout';
const msgs = await Stores.WF.getMessageSet([msgKey]);
addErrorToast(msgs['msgKey']);
}
};
}
export function pollForNewIntegration() {
return (dispatch) => {
dispatch(setPolling(true));
let tries = 0;
dispatch(
setPollingInterval(
setInterval(
async () => await dispatch(polling(tries++)),
SECOND_IN_MS * 3
)
)
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment