Debian Codegraph @

node/rxjs/src/internal/operators/takeWhile.ts

raw
1 import { OperatorFunction, MonoTypeOperatorFunction, TruthyTypesOf } from '../types';
2 import { operate } from '../util/lift';
3 import { createOperatorSubscriber } from './OperatorSubscriber';
4
5 export function takeWhile<T>(predicate: BooleanConstructor, inclusive: true): MonoTypeOperatorFunction<T>;
6 export function takeWhile<T>(predicate: BooleanConstructor, inclusive: false): OperatorFunction<T, TruthyTypesOf<T>>;
7 export function takeWhile<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
8 export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S): OperatorFunction<T, S>;
9 export function takeWhile<T, S extends T>(predicate: (value: T, index: number) => value is S, inclusive: false): OperatorFunction<T, S>;
10 export function takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive?: boolean): MonoTypeOperatorFunction<T>;
11
12 /**
13 * Emits values emitted by the source Observable so long as each value satisfies
14 * the given `predicate`, and then completes as soon as this `predicate` is not
15 * satisfied.
16 *
17 * <span class="informal">Takes values from the source only while they pass the
18 * condition given. When the first value does not satisfy, it completes.</span>
19 *
20 * ![](takeWhile.png)
21 *
22 * `takeWhile` subscribes and begins mirroring the source Observable. Each value
23 * emitted on the source is given to the `predicate` function which returns a
24 * boolean, representing a condition to be satisfied by the source values. The
25 * output Observable emits the source values until such time as the `predicate`
26 * returns false, at which point `takeWhile` stops mirroring the source
27 * Observable and completes the output Observable.
28 *
29 * ## Example
30 *
31 * Emit click events only while the clientX property is greater than 200
32 *
33 * ```ts
34 * import { fromEvent, takeWhile } from 'rxjs';
35 *
36 * const clicks = fromEvent<PointerEvent>(document, 'click');
37 * const result = clicks.pipe(takeWhile(ev => ev.clientX > 200));
38 * result.subscribe(x => console.log(x));
39 * ```
40 *
41 * @see {@link take}
42 * @see {@link takeLast}
43 * @see {@link takeUntil}
44 * @see {@link skip}
45 *
46 * @param predicate A function that evaluates a value emitted by the source
47 * Observable and returns a boolean. Also takes the (zero-based) index as the
48 * second argument.
49 * @param inclusive When set to `true` the value that caused `predicate` to
50 * return `false` will also be emitted.
51 * @return A function that returns an Observable that emits values from the
52 * source Observable so long as each value satisfies the condition defined by
53 * the `predicate`, then completes.
54 */
55 export function takeWhile<T>(predicate: (value: T, index: number) => boolean, inclusive = false): MonoTypeOperatorFunction<T> {
56 return operate((source, subscriber) => {
57 let index = 0;
58 source.subscribe(
59 createOperatorSubscriber(subscriber, (value) => {
60 const result = predicate(value, index++);
61 (result || inclusive) && subscriber.next(value);
62 !result && subscriber.complete();
63 })
64 );
65 });
66 }