Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Effect()
- loadData$: Observable<Action> = this.actions$
- .ofType(ActionTypes.LOAD_DATA)
- .pluck('payload')
- .switchMap(params => {
- return this.myService.getData(params)
- .map(res => new LoadDataCompleteAction(res))
- // ...and this part would have to be extracted:
- .retryWhen(attempts => Observable
- .zip(attempts, Observable.range(1, 5))
- .flatMap((n, i) => {
- if (i < 4) {
- return Observable.timer(1000 * i);
- } else {
- throw(n);
- }
- })
- )
- })
- .catch(err => Observable.of(new LoadDataFailed()));
- @Effect()
- loadData$: Observable<Action> = this.actions$
- .ofType(ActionTypes.LOAD_DATA)
- .pluck('payload')
- .switchMap(params => {
- return this.myService.getData(params)
- .map(res => new LoadDataCompleteAction(res))
- .retryWhen(attempts => Observable.retryOrThrow(attempts, maxAttempts)
- // or maybe - that's my design question
- .retryOrThrow(attempts, maxAttempts)
- })
- .catch(err => Observable.of(new LoadDataFailed()));
- declare module 'rxjs/Observable' {
- interface Observable<T> {
- retryOrThrow<T>(): Observable<T>;
- }
- }
- Observable.prototype.retryOrThrow = function(attempt, max) {
- console.log('retryOrThrow called');
- return Observable.create(subscriber => {
- const source = this;
- const subscription = source.subscribe(() => {
- // important: catch errors from user-provided callbacks
- try {
- subscriber
- .zip(attempt, Observable.range(1, max + 1))
- .flatMap((n, i) => {
- console.log(n, i);
- if (i < max) {
- return Observable.timer(1000 * i);
- } else {
- throw(n);
- }
- });
- } catch (err) {
- subscriber.error(err);
- }
- },
- // be sure to handle errors and completions as appropriate and send them along
- err => subscriber.error(err),
- () => subscriber.complete());
- // to return now
- return subscription;
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement