Skip to content

Instantly share code, notes, and snippets.

@mordaha
Last active September 21, 2017 14:54
Show Gist options
  • Save mordaha/2c5776f0c8ccd59ebd7c8c9e74c500b5 to your computer and use it in GitHub Desktop.
Save mordaha/2c5776f0c8ccd59ebd7c8c9e74c500b5 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 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'))),
);
};
@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