Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / retryWhen.ts
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';
6
7 import { OuterSubscriber } from '../OuterSubscriber';
8 import { InnerSubscriber } from '../InnerSubscriber';
9 import { subscribeToResult } from '../util/subscribeToResult';
10
11 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
12
13 /**
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.
18  *
19  * ![](retryWhen.png)
20  *
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.
24  * @method retryWhen
25  * @owner Observable
26  */
27 export function retryWhen<T>(notifier: (errors: Observable<any>) => Observable<any>): MonoTypeOperatorFunction<T> {
28   return (source: Observable<T>) => source.lift(new RetryWhenOperator(notifier, source));
29 }
30
31 class RetryWhenOperator<T> implements Operator<T, T> {
32   constructor(protected notifier: (errors: Observable<any>) => Observable<any>,
33               protected source: Observable<T>) {
34   }
35
36   call(subscriber: Subscriber<T>, source: any): TeardownLogic {
37     return source.subscribe(new RetryWhenSubscriber(subscriber, this.notifier, this.source));
38   }
39 }
40
41 /**
42  * We need this JSDoc comment for affecting ESDoc.
43  * @ignore
44  * @extends {Ignored}
45  */
46 class RetryWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
47
48   private errors: Subject<any>;
49   private retries: Observable<any>;
50   private retriesSubscription: Subscription;
51
52   constructor(destination: Subscriber<R>,
53               private notifier: (errors: Observable<any>) => Observable<any>,
54               private source: Observable<T>) {
55     super(destination);
56   }
57
58   error(err: any) {
59     if (!this.isStopped) {
60
61       let errors = this.errors;
62       let retries: any = this.retries;
63       let retriesSubscription = this.retriesSubscription;
64
65       if (!retries) {
66         errors = new Subject();
67         try {
68           const { notifier } = this;
69           retries = notifier(errors);
70         } catch (e) {
71           return super.error(e);
72         }
73         retriesSubscription = subscribeToResult(this, retries);
74       } else {
75         this.errors = null;
76         this.retriesSubscription = null;
77       }
78
79       this._unsubscribeAndRecycle();
80
81       this.errors = errors;
82       this.retries = retries;
83       this.retriesSubscription = retriesSubscription;
84
85       errors.next(err);
86     }
87   }
88
89   /** @deprecated This is an internal implementation detail, do not use. */
90   _unsubscribe() {
91     const { errors, retriesSubscription } = this;
92     if (errors) {
93       errors.unsubscribe();
94       this.errors = null;
95     }
96     if (retriesSubscription) {
97       retriesSubscription.unsubscribe();
98       this.retriesSubscription = null;
99     }
100     this.retries = null;
101   }
102
103   notifyNext(outerValue: T, innerValue: R,
104              outerIndex: number, innerIndex: number,
105              innerSub: InnerSubscriber<T, R>): void {
106     const { _unsubscribe } = this;
107
108     this._unsubscribe = null;
109     this._unsubscribeAndRecycle();
110     this._unsubscribe = _unsubscribe;
111
112     this.source.subscribe(this);
113   }
114 }