3bf6ff4a410d0086168b2be236a5016d99598155
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / exhaustMap.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 exhaustMap<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 exhaustMap<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 exhaustMap<T, I, R>(project: (value: T, index: number) => ObservableInput<I>, resultSelector: (outerValue: T, innerValue: I, 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 only if the previous projected Observable has completed.
23  *
24  * <span class="informal">Maps each value to an Observable, then flattens all of
25  * these inner Observables using {@link exhaust}.</span>
26  *
27  * ![](exhaustMap.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. When it projects a source value to
32  * an Observable, the output Observable begins emitting the items emitted by
33  * that projected Observable. However, `exhaustMap` ignores every new projected
34  * Observable if the previous projected Observable has not yet completed. Once
35  * that one completes, it will accept and flatten the next projected Observable
36  * and repeat this process.
37  *
38  * ## Example
39  * Run a finite timer for each click, only if there is no currently active timer
40  * ```ts
41  * import { fromEvent, interval } from 'rxjs';
42  * import { exhaustMap, take } from 'rxjs/operators';
43  *
44  * const clicks = fromEvent(document, 'click');
45  * const result = clicks.pipe(
46  *   exhaustMap(ev => interval(1000).pipe(take(5)))
47  * );
48  * result.subscribe(x => console.log(x));
49  * ```
50  *
51  * @see {@link concatMap}
52  * @see {@link exhaust}
53  * @see {@link mergeMap}
54  * @see {@link switchMap}
55  *
56  * @param {function(value: T, ?index: number): ObservableInput} project A function
57  * that, when applied to an item emitted by the source Observable, returns an
58  * Observable.
59  * @return {Observable} An Observable containing projected Observables
60  * of each item of the source, ignoring projected Observables that start before
61  * their preceding Observable has completed.
62  * @method exhaustMap
63  * @owner Observable
64  */
65 export function exhaustMap<T, R, O extends ObservableInput<any>>(
66   project: (value: T, index: number) => O,
67   resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R,
68 ): OperatorFunction<T, ObservedValueOf<O>|R> {
69   if (resultSelector) {
70     // DEPRECATED PATH
71     return (source: Observable<T>) => source.pipe(
72       exhaustMap((a, i) => from(project(a, i)).pipe(
73         map((b: any, ii: any) => resultSelector(a, b, i, ii)),
74       )),
75     );
76   }
77   return (source: Observable<T>) =>
78     source.lift(new ExhaustMapOperator(project));
79 }
80
81 class ExhaustMapOperator<T, R> implements Operator<T, R> {
82   constructor(private project: (value: T, index: number) => ObservableInput<R>) {
83   }
84
85   call(subscriber: Subscriber<R>, source: any): any {
86     return source.subscribe(new ExhaustMapSubscriber(subscriber, this.project));
87   }
88 }
89
90 /**
91  * We need this JSDoc comment for affecting ESDoc.
92  * @ignore
93  * @extends {Ignored}
94  */
95 class ExhaustMapSubscriber<T, R> extends OuterSubscriber<T, R> {
96   private hasSubscription = false;
97   private hasCompleted = false;
98   private index = 0;
99
100   constructor(destination: Subscriber<R>,
101               private project: (value: T, index: number) => ObservableInput<R>) {
102     super(destination);
103   }
104
105   protected _next(value: T): void {
106     if (!this.hasSubscription) {
107       this.tryNext(value);
108     }
109   }
110
111   private tryNext(value: T): void {
112     let result: ObservableInput<R>;
113     const index = this.index++;
114     try {
115       result = this.project(value, index);
116     } catch (err) {
117       this.destination.error(err);
118       return;
119     }
120     this.hasSubscription = true;
121     this._innerSub(result, value, index);
122   }
123
124   private _innerSub(result: ObservableInput<R>, value: T, index: number): void {
125     const innerSubscriber = new InnerSubscriber(this, value, index);
126     const destination = this.destination as Subscription;
127     destination.add(innerSubscriber);
128     const innerSubscription = subscribeToResult<T, R>(this, result, undefined, undefined, innerSubscriber);
129     // The returned subscription will usually be the subscriber that was
130     // passed. However, interop subscribers will be wrapped and for
131     // unsubscriptions to chain correctly, the wrapper needs to be added, too.
132     if (innerSubscription !== innerSubscriber) {
133       destination.add(innerSubscription);
134     }
135   }
136
137   protected _complete(): void {
138     this.hasCompleted = true;
139     if (!this.hasSubscription) {
140       this.destination.complete();
141     }
142     this.unsubscribe();
143   }
144
145   notifyNext(outerValue: T, innerValue: R,
146              outerIndex: number, innerIndex: number,
147              innerSub: InnerSubscriber<T, R>): void {
148     this.destination.next(innerValue);
149   }
150
151   notifyError(err: any): void {
152     this.destination.error(err);
153   }
154
155   notifyComplete(innerSub: Subscription): void {
156     const destination = this.destination as Subscription;
157     destination.remove(innerSub);
158
159     this.hasSubscription = false;
160     if (this.hasCompleted) {
161       this.destination.complete();
162     }
163   }
164 }