Debian Codegraph @

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

raw
1 import { MonoTypeOperatorFunction, ObservableInput } from '../types';
2 import { operate } from '../util/lift';
3 import { createOperatorSubscriber } from './OperatorSubscriber';
4 import { innerFrom } from '../observable/innerFrom';
5 import { noop } from '../util/noop';
6
7 /**
8 * Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item.
9 *
10 * The `skipUntil` operator causes the observable stream to skip the emission of values until the passed in observable
11 * emits the first value. This can be particularly useful in combination with user interactions, responses of HTTP
12 * requests or waiting for specific times to pass by.
13 *
14 * ![](skipUntil.png)
15 *
16 * Internally, the `skipUntil` operator subscribes to the passed in `notifier` `ObservableInput` (which gets converted
17 * to an Observable) in order to recognize the emission of its first value. When `notifier` emits next, the operator
18 * unsubscribes from it and starts emitting the values of the *source* observable until it completes or errors. It
19 * will never let the *source* observable emit any values if the `notifier` completes or throws an error without
20 * emitting a value before.
21 *
22 * ## Example
23 *
24 * In the following example, all emitted values of the interval observable are skipped until the user clicks anywhere
25 * within the page
26 *
27 * ```ts
28 * import { interval, fromEvent, skipUntil } from 'rxjs';
29 *
30 * const intervalObservable = interval(1000);
31 * const click = fromEvent(document, 'click');
32 *
33 * const emitAfterClick = intervalObservable.pipe(
34 * skipUntil(click)
35 * );
36 * // clicked at 4.6s. output: 5...6...7...8........ or
37 * // clicked at 7.3s. output: 8...9...10..11.......
38 * emitAfterClick.subscribe(value => console.log(value));
39 * ```
40 *
41 * @see {@link last}
42 * @see {@link skip}
43 * @see {@link skipWhile}
44 * @see {@link skipLast}
45 *
46 * @param notifier An `ObservableInput` that has to emit an item before the source Observable elements begin to
47 * be mirrored by the resulting Observable.
48 * @return A function that returns an Observable that skips items from the
49 * source Observable until the `notifier` Observable emits an item, then emits the
50 * remaining items.
51 */
52 export function skipUntil<T>(notifier: ObservableInput<any>): MonoTypeOperatorFunction<T> {
53 return operate((source, subscriber) => {
54 let taking = false;
55
56 const skipSubscriber = createOperatorSubscriber(
57 subscriber,
58 () => {
59 skipSubscriber?.unsubscribe();
60 taking = true;
61 },
62 noop
63 );
64
65 innerFrom(notifier).subscribe(skipSubscriber);
66
67 source.subscribe(createOperatorSubscriber(subscriber, (value) => taking && subscriber.next(value)));
68 });
69 }