Skip to content

Instantly share code, notes, and snippets.

@mordaha
Last active September 21, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mordaha/b161a944209a10002975c013ad478c99 to your computer and use it in GitHub Desktop.
Save mordaha/b161a944209a10002975c013ad478c99 to your computer and use it in GitHub Desktop.
// @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'))),
);
};
@mordaha
Copy link
Author

mordaha commented Sep 21, 2017

        <Button onPress={() => this.props.dispatch({ type: 'FETCH_EPIC', payload: Date.now() })}>
          Epic
        </Button>
        <Button onPress={() => this.props.dispatch({ type: 'CANCEL_EPIC', payload: Date.now() })}>
          Cancel Epic
        </Button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment