Last active
September 21, 2017 14:54
-
-
Save mordaha/2c5776f0c8ccd59ebd7c8c9e74c500b5 to your computer and use it in GitHub Desktop.
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
// @flow | |
import Rxjs from 'rxjs'; | |
import { ajax } from 'rxjs/observable/dom/ajax'; | |
const { Observable } = Rxjs; | |
const retryActionAfterDelaySeconds = (action, error, delaySeconds = 5) => { | |
const retryAction = { ...action, retry: (action.retry || 0) + 1 }; | |
return Observable.timer(0, 1000).map( | |
s => | |
(s < delaySeconds | |
? { | |
type: 'SECONDS_TO_RETRY', | |
payload: delaySeconds - s, | |
} | |
: retryAction), | |
); | |
}; | |
export const testEpic = (action$: Object) => { | |
return action$ | |
.do(x => x.type === 'SECONDS_TO_RETRY' && console.log('Seconds to retry: ', x.payload)) | |
.filter(action => action.type === 'FETCH_EPIC') | |
.do(x => console.log('EPIC ACTION ', x)) | |
.mergeMap( | |
action => | |
(action.error | |
? Observable.of({ type: 'FETCH_ERROR', payload: action.payload }) | |
: ajax | |
.getJSON('https://yandex.ru/123') | |
.map(r => ({ type: 'UPDATE_EPIC', payload: r })) | |
.catch(e => retryActionAfterDelaySeconds(action, e, 5)) | |
.takeUntil(action$.ofType('FETCH_EPIC')) | |
.takeUntil(action$.ofType('CANCEL_EPIC'))), | |
); | |
}; |
Author
mordaha
commented
Sep 21, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment