84ea1c7fbf82deb59aaee5c6b90bf5822c859f05
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / takeUntil.ts
1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4
5 import { OuterSubscriber } from '../OuterSubscriber';
6 import { InnerSubscriber } from '../InnerSubscriber';
7 import { subscribeToResult } from '../util/subscribeToResult';
8
9 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
10
11 /**
12  * Emits the values emitted by the source Observable until a `notifier`
13  * Observable emits a value.
14  *
15  * <span class="informal">Lets values pass until a second Observable,
16  * `notifier`, emits a value. Then, it completes.</span>
17  *
18  * ![](takeUntil.png)
19  *
20  * `takeUntil` subscribes and begins mirroring the source Observable. It also
21  * monitors a second Observable, `notifier` that you provide. If the `notifier`
22  * emits a value, the output Observable stops mirroring the source Observable
23  * and completes. If the `notifier` doesn't emit any value and completes
24  * then `takeUntil` will pass all values.
25  *
26  * ## Example
27  * Tick every second until the first click happens
28  * ```ts
29  * import { fromEvent, interval } from 'rxjs';
30  * import { takeUntil } from 'rxjs/operators';
31  *
32  * const source = interval(1000);
33  * const clicks = fromEvent(document, 'click');
34  * const result = source.pipe(takeUntil(clicks));
35  * result.subscribe(x => console.log(x));
36  * ```
37  *
38  * @see {@link take}
39  * @see {@link takeLast}
40  * @see {@link takeWhile}
41  * @see {@link skip}
42  *
43  * @param {Observable} notifier The Observable whose first emitted value will
44  * cause the output Observable of `takeUntil` to stop emitting values from the
45  * source Observable.
46  * @return {Observable<T>} An Observable that emits the values from the source
47  * Observable until such time as `notifier` emits its first value.
48  * @method takeUntil
49  * @owner Observable
50  */
51 export function takeUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
52   return (source: Observable<T>) => source.lift(new TakeUntilOperator(notifier));
53 }
54
55 class TakeUntilOperator<T> implements Operator<T, T> {
56   constructor(private notifier: Observable<any>) {
57   }
58
59   call(subscriber: Subscriber<T>, source: any): TeardownLogic {
60     const takeUntilSubscriber = new TakeUntilSubscriber(subscriber);
61     const notifierSubscription = subscribeToResult(takeUntilSubscriber, this.notifier);
62     if (notifierSubscription && !takeUntilSubscriber.seenValue) {
63       takeUntilSubscriber.add(notifierSubscription);
64       return source.subscribe(takeUntilSubscriber);
65     }
66     return takeUntilSubscriber;
67   }
68 }
69
70 /**
71  * We need this JSDoc comment for affecting ESDoc.
72  * @ignore
73  * @extends {Ignored}
74  */
75 class TakeUntilSubscriber<T, R> extends OuterSubscriber<T, R> {
76   seenValue = false;
77
78   constructor(destination: Subscriber<any>, ) {
79     super(destination);
80   }
81
82   notifyNext(outerValue: T, innerValue: R,
83              outerIndex: number, innerIndex: number,
84              innerSub: InnerSubscriber<T, R>): void {
85     this.seenValue = true;
86     this.complete();
87   }
88
89   notifyComplete(): void {
90     // noop
91   }
92 }