1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { Subject } from '../Subject';
5 import { Subscription } from '../Subscription';
7 import { OuterSubscriber } from '../OuterSubscriber';
8 import { InnerSubscriber } from '../InnerSubscriber';
9 import { subscribeToResult } from '../util/subscribeToResult';
11 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
14 * Returns an Observable that mirrors the source Observable with the exception of an `error`. If the source Observable
15 * calls `error`, this method will emit the Throwable that caused the error to the Observable returned from `notifier`.
16 * If that Observable calls `complete` or `error` then this method will call `complete` or `error` on the child
17 * subscription. Otherwise this method will resubscribe to the source Observable.
21 * @param {function(errors: Observable): Observable} notifier - Receives an Observable of notifications with which a
22 * user can `complete` or `error`, aborting the retry.
23 * @return {Observable} The source Observable modified with retry logic.
27 export function retryWhen<T>(notifier: (errors: Observable<any>) => Observable<any>): MonoTypeOperatorFunction<T> {
28 return (source: Observable<T>) => source.lift(new RetryWhenOperator(notifier, source));
31 class RetryWhenOperator<T> implements Operator<T, T> {
32 constructor(protected notifier: (errors: Observable<any>) => Observable<any>,
33 protected source: Observable<T>) {
36 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
37 return source.subscribe(new RetryWhenSubscriber(subscriber, this.notifier, this.source));
42 * We need this JSDoc comment for affecting ESDoc.
46 class RetryWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
48 private errors: Subject<any>;
49 private retries: Observable<any>;
50 private retriesSubscription: Subscription;
52 constructor(destination: Subscriber<R>,
53 private notifier: (errors: Observable<any>) => Observable<any>,
54 private source: Observable<T>) {
59 if (!this.isStopped) {
61 let errors = this.errors;
62 let retries: any = this.retries;
63 let retriesSubscription = this.retriesSubscription;
66 errors = new Subject();
68 const { notifier } = this;
69 retries = notifier(errors);
71 return super.error(e);
73 retriesSubscription = subscribeToResult(this, retries);
76 this.retriesSubscription = null;
79 this._unsubscribeAndRecycle();
82 this.retries = retries;
83 this.retriesSubscription = retriesSubscription;
89 /** @deprecated This is an internal implementation detail, do not use. */
91 const { errors, retriesSubscription } = this;
96 if (retriesSubscription) {
97 retriesSubscription.unsubscribe();
98 this.retriesSubscription = null;
103 notifyNext(outerValue: T, innerValue: R,
104 outerIndex: number, innerIndex: number,
105 innerSub: InnerSubscriber<T, R>): void {
106 const { _unsubscribe } = this;
108 this._unsubscribe = null;
109 this._unsubscribeAndRecycle();
110 this._unsubscribe = _unsubscribe;
112 this.source.subscribe(this);