c3c831ad881b42a3c0c80391b6aab21b011b4035
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / switchMap.ts
1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { OuterSubscriber } from '../OuterSubscriber';
6 import { InnerSubscriber } from '../InnerSubscriber';
7 import { subscribeToResult } from '../util/subscribeToResult';
8 import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
9 import { map } from './map';
10 import { from } from '../observable/from';
11
12 /* tslint:disable:max-line-length */
13 export function switchMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O): OperatorFunction<T, ObservedValueOf<O>>;
14 /** @deprecated resultSelector is no longer supported, use inner map instead */
15 export function switchMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: undefined): OperatorFunction<T, ObservedValueOf<O>>;
16 /** @deprecated resultSelector is no longer supported, use inner map instead */
17 export function switchMap<T, R, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, R>;
18 /* tslint:enable:max-line-length */
19
20 /**
21  * Projects each source value to an Observable which is merged in the output
22  * Observable, emitting values only from the most recently projected Observable.
23  *
24  * <span class="informal">Maps each value to an Observable, then flattens all of
25  * these inner Observables.</span>
26  *
27  * ![](switchMap.png)
28  *
29  * Returns an Observable that emits items based on applying a function that you
30  * supply to each item emitted by the source Observable, where that function
31  * returns an (so-called "inner") Observable. Each time it observes one of these
32  * inner Observables, the output Observable begins emitting the items emitted by
33  * that inner Observable. When a new inner Observable is emitted, `switchMap`
34  * stops emitting items from the earlier-emitted inner Observable and begins
35  * emitting items from the new one. It continues to behave like this for
36  * subsequent inner Observables.
37  *
38  * ## Example
39  * Generate new Observable according to source Observable values
40  * ```typescript
41  * import { of } from 'rxjs';
42  * import { switchMap } from 'rxjs/operators';
43  *
44  * const switched = of(1, 2, 3).pipe(switchMap((x: number) => of(x, x ** 2, x ** 3)));
45  * switched.subscribe(x => console.log(x));
46  * // outputs
47  * // 1
48  * // 1
49  * // 1
50  * // 2
51  * // 4
52  * // 8
53  * // ... and so on
54  * ```
55  *
56  * Rerun an interval Observable on every click event
57  * ```ts
58  * import { fromEvent, interval } from 'rxjs';
59  * import { switchMap } from 'rxjs/operators';
60  *
61  * const clicks = fromEvent(document, 'click');
62  * const result = clicks.pipe(switchMap((ev) => interval(1000)));
63  * result.subscribe(x => console.log(x));
64  * ```
65  *
66  * @see {@link concatMap}
67  * @see {@link exhaustMap}
68  * @see {@link mergeMap}
69  * @see {@link switchAll}
70  * @see {@link switchMapTo}
71  *
72  * @param {function(value: T, ?index: number): ObservableInput} project A function
73  * that, when applied to an item emitted by the source Observable, returns an
74  * Observable.
75  * @return {Observable} An Observable that emits the result of applying the
76  * projection function (and the optional deprecated `resultSelector`) to each item
77  * emitted by the source Observable and taking only the values from the most recently
78  * projected inner Observable.
79  * @method switchMap
80  * @owner Observable
81  */
82 export function switchMap<T, R, O extends ObservableInput<any>>(
83   project: (value: T, index: number) => O,
84   resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R,
85 ): OperatorFunction<T, ObservedValueOf<O>|R> {
86   if (typeof resultSelector === 'function') {
87     return (source: Observable<T>) => source.pipe(
88       switchMap((a, i) => from(project(a, i)).pipe(
89         map((b, ii) => resultSelector(a, b, i, ii))
90       ))
91     );
92   }
93   return (source: Observable<T>) => source.lift(new SwitchMapOperator(project));
94 }
95
96 class SwitchMapOperator<T, R> implements Operator<T, R> {
97   constructor(private project: (value: T, index: number) => ObservableInput<R>) {
98   }
99
100   call(subscriber: Subscriber<R>, source: any): any {
101     return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
102   }
103 }
104
105 /**
106  * We need this JSDoc comment for affecting ESDoc.
107  * @ignore
108  * @extends {Ignored}
109  */
110 class SwitchMapSubscriber<T, R> extends OuterSubscriber<T, R> {
111   private index: number = 0;
112   private innerSubscription: Subscription;
113
114   constructor(destination: Subscriber<R>,
115               private project: (value: T, index: number) => ObservableInput<R>) {
116     super(destination);
117   }
118
119   protected _next(value: T) {
120     let result: ObservableInput<R>;
121     const index = this.index++;
122     try {
123       result = this.project(value, index);
124     } catch (error) {
125       this.destination.error(error);
126       return;
127     }
128     this._innerSub(result, value, index);
129   }
130
131   private _innerSub(result: ObservableInput<R>, value: T, index: number) {
132     const innerSubscription = this.innerSubscription;
133     if (innerSubscription) {
134       innerSubscription.unsubscribe();
135     }
136     const innerSubscriber = new InnerSubscriber(this, value, index);
137     const destination = this.destination as Subscription;
138     destination.add(innerSubscriber);
139     this.innerSubscription = subscribeToResult(this, result, undefined, undefined, innerSubscriber);
140     // The returned subscription will usually be the subscriber that was
141     // passed. However, interop subscribers will be wrapped and for
142     // unsubscriptions to chain correctly, the wrapper needs to be added, too.
143     if (this.innerSubscription !== innerSubscriber) {
144       destination.add(this.innerSubscription);
145     }
146   }
147
148   protected _complete(): void {
149     const {innerSubscription} = this;
150     if (!innerSubscription || innerSubscription.closed) {
151       super._complete();
152     }
153     this.unsubscribe();
154   }
155
156   protected _unsubscribe() {
157     this.innerSubscription = null;
158   }
159
160   notifyComplete(innerSub: Subscription): void {
161     const destination = this.destination as Subscription;
162     destination.remove(innerSub);
163     this.innerSubscription = null;
164     if (this.isStopped) {
165       super._complete();
166     }
167   }
168
169   notifyNext(outerValue: T, innerValue: R,
170              outerIndex: number, innerIndex: number,
171              innerSub: InnerSubscriber<T, R>): void {
172       this.destination.next(innerValue);
173   }
174 }