Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / takeWhile.ts
1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';
5
6 export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S): OperatorFunction<T, S>;
7 export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S, inclusive: false): OperatorFunction<T, S>;
8 export function takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive?: boolean): MonoTypeOperatorFunction<T>;
9
10 /**
11  * Emits values emitted by the source Observable so long as each value satisfies
12  * the given `predicate`, and then completes as soon as this `predicate` is not
13  * satisfied.
14  *
15  * <span class="informal">Takes values from the source only while they pass the
16  * condition given. When the first value does not satisfy, it completes.</span>
17  *
18  * ![](takeWhile.png)
19  *
20  * `takeWhile` subscribes and begins mirroring the source Observable. Each value
21  * emitted on the source is given to the `predicate` function which returns a
22  * boolean, representing a condition to be satisfied by the source values. The
23  * output Observable emits the source values until such time as the `predicate`
24  * returns false, at which point `takeWhile` stops mirroring the source
25  * Observable and completes the output Observable.
26  *
27  * ## Example
28  * Emit click events only while the clientX property is greater than 200
29  * ```ts
30  * import { fromEvent } from 'rxjs';
31  * import { takeWhile } from 'rxjs/operators';
32  *
33  * const clicks = fromEvent(document, 'click');
34  * const result = clicks.pipe(takeWhile(ev => ev.clientX > 200));
35  * result.subscribe(x => console.log(x));
36  * ```
37  *
38  * @see {@link take}
39  * @see {@link takeLast}
40  * @see {@link takeUntil}
41  * @see {@link skip}
42  *
43  * @param {function(value: T, index: number): boolean} predicate A function that
44  * evaluates a value emitted by the source Observable and returns a boolean.
45  * Also takes the (zero-based) index as the second argument.
46  * @param {boolean} inclusive When set to `true` the value that caused
47  * `predicate` to return `false` will also be emitted.
48  * @return {Observable<T>} An Observable that emits the values from the source
49  * Observable so long as each value satisfies the condition defined by the
50  * `predicate`, then completes.
51  * @method takeWhile
52  * @owner Observable
53  */
54 export function takeWhile<T>(
55     predicate: (value: T, index: number) => boolean,
56     inclusive = false): MonoTypeOperatorFunction<T> {
57   return (source: Observable<T>) =>
58              source.lift(new TakeWhileOperator(predicate, inclusive));
59 }
60
61 class TakeWhileOperator<T> implements Operator<T, T> {
62   constructor(
63       private predicate: (value: T, index: number) => boolean,
64       private inclusive: boolean) {}
65
66   call(subscriber: Subscriber<T>, source: any): TeardownLogic {
67     return source.subscribe(
68         new TakeWhileSubscriber(subscriber, this.predicate, this.inclusive));
69   }
70 }
71
72 /**
73  * We need this JSDoc comment for affecting ESDoc.
74  * @ignore
75  * @extends {Ignored}
76  */
77 class TakeWhileSubscriber<T> extends Subscriber<T> {
78   private index: number = 0;
79
80   constructor(
81       destination: Subscriber<T>,
82       private predicate: (value: T, index: number) => boolean,
83       private inclusive: boolean) {
84     super(destination);
85   }
86
87   protected _next(value: T): void {
88     const destination = this.destination;
89     let result: boolean;
90     try {
91       result = this.predicate(value, this.index++);
92     } catch (err) {
93       destination.error(err);
94       return;
95     }
96     this.nextOrComplete(value, result);
97   }
98
99   private nextOrComplete(value: T, predicateResult: boolean): void {
100     const destination = this.destination;
101     if (Boolean(predicateResult)) {
102       destination.next(value);
103     } else {
104       if (this.inclusive) {
105         destination.next(value);
106       }
107       destination.complete();
108     }
109   }
110 }