8310adca587653d2176259b116afd94bebf89f27
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / skipUntil.ts
1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { OuterSubscriber } from '../OuterSubscriber';
5 import { InnerSubscriber } from '../InnerSubscriber';
6 import { subscribeToResult } from '../util/subscribeToResult';
7 import { MonoTypeOperatorFunction, TeardownLogic, ObservableInput } from '../types';
8 import { Subscription } from '../Subscription';
9
10 /**
11  * Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item.
12  *
13  * The `skipUntil` operator causes the observable stream to skip the emission of values ​​until the passed in observable emits the first value.
14  * This can be particularly useful in combination with user interactions, responses of http requests or waiting for specific times to pass by.
15  *
16  * ![](skipUntil.png)
17  *
18  * Internally the `skipUntil` operator subscribes to the passed in observable (in the following called *notifier*) in order to recognize the emission
19  * of its first value. When this happens, the operator unsubscribes from the *notifier* and starts emitting the values of the *source*
20  * observable. It will never let the *source* observable emit any values if the *notifier* completes or throws an error without emitting
21  * a value before.
22  *
23  * ## Example
24  *
25  * In the following example, all emitted values ​​of the interval observable are skipped until the user clicks anywhere within the page.
26  *
27  * ```ts
28  * import { interval, fromEvent } from 'rxjs';
29  * import { skipUntil } from 'rxjs/operators';
30  *
31  * const intervalObservable = interval(1000);
32  * const click = fromEvent(document, 'click');
33  *
34  * const emitAfterClick = intervalObservable.pipe(
35  *   skipUntil(click)
36  * );
37  * // clicked at 4.6s. output: 5...6...7...8........ or
38  * // clicked at 7.3s. output: 8...9...10..11.......
39  * const subscribe = emitAfterClick.subscribe(value => console.log(value));
40  * ```
41  *
42  * @param {Observable} notifier - The second Observable that has to emit an item before the source Observable's elements begin to
43  * be mirrored by the resulting Observable.
44  * @return {Observable<T>} An Observable that skips items from the source Observable until the second Observable emits
45  * an item, then emits the remaining items.
46  * @method skipUntil
47  * @owner Observable
48  */
49 export function skipUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
50   return (source: Observable<T>) => source.lift(new SkipUntilOperator(notifier));
51 }
52
53 class SkipUntilOperator<T> implements Operator<T, T> {
54   constructor(private notifier: Observable<any>) {
55   }
56
57   call(destination: Subscriber<T>, source: any): TeardownLogic {
58     return source.subscribe(new SkipUntilSubscriber(destination, this.notifier));
59   }
60 }
61
62 /**
63  * We need this JSDoc comment for affecting ESDoc.
64  * @ignore
65  * @extends {Ignored}
66  */
67 class SkipUntilSubscriber<T, R> extends OuterSubscriber<T, R> {
68
69   private hasValue: boolean = false;
70   private innerSubscription: Subscription;
71
72   constructor(destination: Subscriber<R>, notifier: ObservableInput<any>) {
73     super(destination);
74     const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
75     this.add(innerSubscriber);
76     this.innerSubscription = innerSubscriber;
77     const innerSubscription = subscribeToResult(this, notifier, undefined, undefined, innerSubscriber);
78     // The returned subscription will usually be the subscriber that was
79     // passed. However, interop subscribers will be wrapped and for
80     // unsubscriptions to chain correctly, the wrapper needs to be added, too.
81     if (innerSubscription !== innerSubscriber) {
82       this.add(innerSubscription);
83       this.innerSubscription = innerSubscription;
84     }
85   }
86
87   protected _next(value: T) {
88     if (this.hasValue) {
89       super._next(value);
90     }
91   }
92
93   notifyNext(outerValue: T, innerValue: R,
94              outerIndex: number, innerIndex: number,
95              innerSub: InnerSubscriber<T, R>): void {
96     this.hasValue = true;
97     if (this.innerSubscription) {
98       this.innerSubscription.unsubscribe();
99     }
100   }
101
102   notifyComplete() {
103     /* do nothing */
104   }
105 }