Skip to content

Instantly share code, notes, and snippets.

@dmtrmrtnv
Created November 28, 2017 13:40
Show Gist options
  • Save dmtrmrtnv/7bdfaa5bf34f086653c1df02b75fc9e1 to your computer and use it in GitHub Desktop.
Save dmtrmrtnv/7bdfaa5bf34f086653c1df02b75fc9e1 to your computer and use it in GitHub Desktop.
Redux-observable epic with polling.
const loadGames = (action$, store, scheduler, timeout = 1000 * 30, debounce = 350) =>
action$
.ofType('LOAD_GAMES')
.debounceTime(debounce, scheduler)
.switchMap(({ payload: { date } }) =>
of({
type: 'SET_GAMES_LOADING', payload: { date },
})
.merge(ajax({
url: `${url}date/${date}`,
responseType: 'json',
})
.map(({ response }) => ({
type: 'RECEIVE_GAMES', payload: { date, items: response },
}))
.repeatWhen(o => o.concatMap(() => timer(timeout, timeout, scheduler)))
.retryWhen(e => e.delay(timeout, scheduler))
.takeUntil(action$.ofType('LOAD_GAMES_CANCEL'))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment