Last active
September 21, 2017 14:44
-
-
Save mordaha/b161a944209a10002975c013ad478c99 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 retryActionOnError = (action, error, retryCount = 0, delay = 5000) => { | |
if (retryCount && action.retry && action.retry >= retryCount) { | |
return Observable.of({ ...action, payload: error, error: true }); | |
} | |
return Observable.of({ ...action, retry: (action.retry || 0) + 1 }).delay(delay); | |
}; | |
export const testEpic = (action$: Object) => { | |
return action$ | |
.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 => retryActionOnError(action, e, 3, 1000)) | |
.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