Skip to content

Instantly share code, notes, and snippets.

@mordaha
Last active Sep 21, 2017
Embed
What would you like to do?
// @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>

Loading

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