Debian Codegraph @

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

raw
1 import { Falsy, MonoTypeOperatorFunction, OperatorFunction } from '../types';
2 import { operate } from '../util/lift';
3 import { createOperatorSubscriber } from './OperatorSubscriber';
4
5 export function skipWhile<T>(predicate: BooleanConstructor): OperatorFunction<T, Extract<T, Falsy> extends never ? never : T>;
6 export function skipWhile<T>(predicate: (value: T, index: number) => true): OperatorFunction<T, never>;
7 export function skipWhile<T>(predicate: (value: T, index: number) => boolean): MonoTypeOperatorFunction<T>;
8
9 /**
10 * Returns an Observable that skips all items emitted by the source Observable as long as a specified condition holds
11 * true, but emits all further source items as soon as the condition becomes false.
12 *
13 * ![](skipWhile.png)
14 *
15 * Skips all the notifications with a truthy predicate. It will not skip the notifications when the predicate is falsy.
16 * It can also be skipped using index. Once the predicate is true, it will not be called again.
17 *
18 * ## Example
19 *
20 * Skip some super heroes
21 *
22 * ```ts
23 * import { from, skipWhile } from 'rxjs';
24 *
25 * const source = from(['Green Arrow', 'SuperMan', 'Flash', 'SuperGirl', 'Black Canary'])
26 * // Skip the heroes until SuperGirl
27 * const example = source.pipe(skipWhile(hero => hero !== 'SuperGirl'));
28 * // output: SuperGirl, Black Canary
29 * example.subscribe(femaleHero => console.log(femaleHero));
30 * ```
31 *
32 * Skip values from the array until index 5
33 *
34 * ```ts
35 * import { from, skipWhile } from 'rxjs';
36 *
37 * const source = from([1, 2, 3, 4, 5, 6, 7, 9, 10]);
38 * const example = source.pipe(skipWhile((_, i) => i !== 5));
39 * // output: 6, 7, 9, 10
40 * example.subscribe(value => console.log(value));
41 * ```
42 *
43 * @see {@link last}
44 * @see {@link skip}
45 * @see {@link skipUntil}
46 * @see {@link skipLast}
47 *
48 * @param predicate A function to test each item emitted from the source Observable.
49 * @return A function that returns an Observable that begins emitting items
50 * emitted by the source Observable when the specified predicate becomes false.
51 */
52 export function skipWhile<T>(predicate: (value: T, index: number) => boolean): MonoTypeOperatorFunction<T> {
53 return operate((source, subscriber) => {
54 let taking = false;
55 let index = 0;
56 source.subscribe(
57 createOperatorSubscriber(subscriber, (value) => (taking || (taking = !predicate(value, index++))) && subscriber.next(value))
58 );
59 });
60 }