058ca8ec0cd0aba1435d7f6f6cdbb4bce6c174e7
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / onErrorResumeNext.ts
1 import { Observable } from '../Observable';
2 import { from } from '../observable/from';
3 import { Operator } from '../Operator';
4 import { Subscriber } from '../Subscriber';
5 import { Subscription } from '../Subscription';
6 import { isArray } from '../util/isArray';
7 import { OuterSubscriber } from '../OuterSubscriber';
8 import { InnerSubscriber } from '../InnerSubscriber';
9 import { subscribeToResult } from '../util/subscribeToResult';
10 import { ObservableInput, OperatorFunction } from '../types';
11
12 /* tslint:disable:max-line-length */
13 export function onErrorResumeNext<T>(): OperatorFunction<T, T>;
14 export function onErrorResumeNext<T, T2>(v: ObservableInput<T2>): OperatorFunction<T, T | T2>;
15 export function onErrorResumeNext<T, T2, T3>(v: ObservableInput<T2>, v2: ObservableInput<T3>): OperatorFunction<T, T | T2 | T3>;
16 export function onErrorResumeNext<T, T2, T3, T4>(v: ObservableInput<T2>, v2: ObservableInput<T3>, v3: ObservableInput<T4>): OperatorFunction<T, T | T2 | T3 | T4>;
17 export function onErrorResumeNext<T, T2, T3, T4, T5>(v: ObservableInput<T2>, v2: ObservableInput<T3>, v3: ObservableInput<T4>, v4: ObservableInput<T5>): OperatorFunction<T, T | T2 | T3 | T4 | T5>;
18 export function onErrorResumeNext<T, T2, T3, T4, T5, T6>(v: ObservableInput<T2>, v2: ObservableInput<T3>, v3: ObservableInput<T4>, v4: ObservableInput<T5>, v5: ObservableInput<T6>): OperatorFunction<T, T | T2 | T3 | T4 | T5 | T6>;
19 export function onErrorResumeNext<T, T2, T3, T4, T5, T6, T7>(v: ObservableInput<T2>, v2: ObservableInput<T3>, v3: ObservableInput<T4>, v4: ObservableInput<T5>, v5: ObservableInput<T6>, v6: ObservableInput<T7>): OperatorFunction<T, T | T2 | T3 | T4 | T5 | T6 | T7>;
20 export function onErrorResumeNext<T, R>(...observables: Array<ObservableInput<any>>): OperatorFunction<T, T | R>;
21 export function onErrorResumeNext<T, R>(array: ObservableInput<any>[]): OperatorFunction<T, T | R>;
22 /* tslint:enable:max-line-length */
23
24 /**
25  * When any of the provided Observable emits an complete or error notification, it immediately subscribes to the next one
26  * that was passed.
27  *
28  * <span class="informal">Execute series of Observables no matter what, even if it means swallowing errors.</span>
29  *
30  * ![](onErrorResumeNext.png)
31  *
32  * `onErrorResumeNext` is an operator that accepts a series of Observables, provided either directly as
33  * arguments or as an array. If no single Observable is provided, returned Observable will simply behave the same
34  * as the source.
35  *
36  * `onErrorResumeNext` returns an Observable that starts by subscribing and re-emitting values from the source Observable.
37  * When its stream of values ends - no matter if Observable completed or emitted an error - `onErrorResumeNext`
38  * will subscribe to the first Observable that was passed as an argument to the method. It will start re-emitting
39  * its values as well and - again - when that stream ends, `onErrorResumeNext` will proceed to subscribing yet another
40  * Observable in provided series, no matter if previous Observable completed or ended with an error. This will
41  * be happening until there is no more Observables left in the series, at which point returned Observable will
42  * complete - even if the last subscribed stream ended with an error.
43  *
44  * `onErrorResumeNext` can be therefore thought of as version of {@link concat} operator, which is more permissive
45  * when it comes to the errors emitted by its input Observables. While `concat` subscribes to the next Observable
46  * in series only if previous one successfully completed, `onErrorResumeNext` subscribes even if it ended with
47  * an error.
48  *
49  * Note that you do not get any access to errors emitted by the Observables. In particular do not
50  * expect these errors to appear in error callback passed to {@link Observable#subscribe}. If you want to take
51  * specific actions based on what error was emitted by an Observable, you should try out {@link catchError} instead.
52  *
53  *
54  * ## Example
55  * Subscribe to the next Observable after map fails
56  * ```ts
57  * import { of } from 'rxjs';
58  * import { onErrorResumeNext, map } from 'rxjs/operators';
59  *
60  * of(1, 2, 3, 0).pipe(
61  *   map(x => {
62  *       if (x === 0) { throw Error(); }
63  *        return 10 / x;
64  *   }),
65  *   onErrorResumeNext(of(1, 2, 3)),
66  * )
67  * .subscribe(
68  *   val => console.log(val),
69  *   err => console.log(err),          // Will never be called.
70  *   () => console.log('that\'s it!')
71  * );
72  *
73  * // Logs:
74  * // 10
75  * // 5
76  * // 3.3333333333333335
77  * // 1
78  * // 2
79  * // 3
80  * // "that's it!"
81  * ```
82  *
83  * @see {@link concat}
84  * @see {@link catchError}
85  *
86  * @param {...ObservableInput} observables Observables passed either directly or as an array.
87  * @return {Observable} An Observable that emits values from source Observable, but - if it errors - subscribes
88  * to the next passed Observable and so on, until it completes or runs out of Observables.
89  * @method onErrorResumeNext
90  * @owner Observable
91  */
92
93 export function onErrorResumeNext<T, R>(...nextSources: Array<ObservableInput<any> |
94                                                        Array<ObservableInput<any>>>): OperatorFunction<T, R> {
95   if (nextSources.length === 1 && isArray(nextSources[0])) {
96     nextSources = <Array<Observable<any>>>nextSources[0];
97   }
98
99   return (source: Observable<T>) => source.lift(new OnErrorResumeNextOperator<T, R>(nextSources));
100 }
101
102 /* tslint:disable:max-line-length */
103 export function onErrorResumeNextStatic<R>(v: ObservableInput<R>): Observable<R>;
104 export function onErrorResumeNextStatic<T2, T3, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<R>;
105 export function onErrorResumeNextStatic<T2, T3, T4, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<R>;
106 export function onErrorResumeNextStatic<T2, T3, T4, T5, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<R>;
107 export function onErrorResumeNextStatic<T2, T3, T4, T5, T6, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<R>;
108
109 export function onErrorResumeNextStatic<R>(...observables: Array<ObservableInput<any> | ((...values: Array<any>) => R)>): Observable<R>;
110 export function onErrorResumeNextStatic<R>(array: ObservableInput<any>[]): Observable<R>;
111 /* tslint:enable:max-line-length */
112
113 export function onErrorResumeNextStatic<T, R>(...nextSources: Array<ObservableInput<any> |
114   Array<ObservableInput<any>> |
115   ((...values: Array<any>) => R)>): Observable<R> {
116   let source: ObservableInput<any> = null;
117
118   if (nextSources.length === 1 && isArray(nextSources[0])) {
119     nextSources = <Array<ObservableInput<any>>>nextSources[0];
120   }
121   source = nextSources.shift();
122
123   return from(source, null).lift(new OnErrorResumeNextOperator<T, R>(nextSources));
124 }
125
126 class OnErrorResumeNextOperator<T, R> implements Operator<T, R> {
127   constructor(private nextSources: Array<ObservableInput<any>>) {
128   }
129
130   call(subscriber: Subscriber<R>, source: any): any {
131     return source.subscribe(new OnErrorResumeNextSubscriber(subscriber, this.nextSources));
132   }
133 }
134
135 class OnErrorResumeNextSubscriber<T, R> extends OuterSubscriber<T, R> {
136   constructor(protected destination: Subscriber<T>,
137               private nextSources: Array<ObservableInput<any>>) {
138     super(destination);
139   }
140
141   notifyError(error: any, innerSub: InnerSubscriber<T, any>): void {
142     this.subscribeToNextSource();
143   }
144
145   notifyComplete(innerSub: InnerSubscriber<T, any>): void {
146     this.subscribeToNextSource();
147   }
148
149   protected _error(err: any): void {
150     this.subscribeToNextSource();
151     this.unsubscribe();
152   }
153
154   protected _complete(): void {
155     this.subscribeToNextSource();
156     this.unsubscribe();
157   }
158
159   private subscribeToNextSource(): void {
160     const next = this.nextSources.shift();
161     if (!!next) {
162       const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
163       const destination = this.destination as Subscription;
164       destination.add(innerSubscriber);
165       const innerSubscription = subscribeToResult(this, next, undefined, undefined, innerSubscriber);
166       // The returned subscription will usually be the subscriber that was
167       // passed. However, interop subscribers will be wrapped and for
168       // unsubscriptions to chain correctly, the wrapper needs to be added, too.
169       if (innerSubscription !== innerSubscriber) {
170         destination.add(innerSubscription);
171       }
172     } else {
173       this.destination.complete();
174     }
175   }
176 }