1 | import { Operator } from '../Operator';
|
---|
2 | import { Observable } from '../Observable';
|
---|
3 | import { Subscriber } from '../Subscriber';
|
---|
4 |
|
---|
5 | import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
|
---|
6 | import { innerSubscribe, SimpleInnerSubscriber, SimpleOuterSubscriber } from '../innerSubscribe';
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Emits the values emitted by the source Observable until a `notifier`
|
---|
10 | * Observable emits a value.
|
---|
11 | *
|
---|
12 | * <span class="informal">Lets values pass until a second Observable,
|
---|
13 | * `notifier`, emits a value. Then, it completes.</span>
|
---|
14 | *
|
---|
15 | * ![](takeUntil.png)
|
---|
16 | *
|
---|
17 | * `takeUntil` subscribes and begins mirroring the source Observable. It also
|
---|
18 | * monitors a second Observable, `notifier` that you provide. If the `notifier`
|
---|
19 | * emits a value, the output Observable stops mirroring the source Observable
|
---|
20 | * and completes. If the `notifier` doesn't emit any value and completes
|
---|
21 | * then `takeUntil` will pass all values.
|
---|
22 | *
|
---|
23 | * ## Example
|
---|
24 | * Tick every second until the first click happens
|
---|
25 | * ```ts
|
---|
26 | * import { fromEvent, interval } from 'rxjs';
|
---|
27 | * import { takeUntil } from 'rxjs/operators';
|
---|
28 | *
|
---|
29 | * const source = interval(1000);
|
---|
30 | * const clicks = fromEvent(document, 'click');
|
---|
31 | * const result = source.pipe(takeUntil(clicks));
|
---|
32 | * result.subscribe(x => console.log(x));
|
---|
33 | * ```
|
---|
34 | *
|
---|
35 | * @see {@link take}
|
---|
36 | * @see {@link takeLast}
|
---|
37 | * @see {@link takeWhile}
|
---|
38 | * @see {@link skip}
|
---|
39 | *
|
---|
40 | * @param {Observable} notifier The Observable whose first emitted value will
|
---|
41 | * cause the output Observable of `takeUntil` to stop emitting values from the
|
---|
42 | * source Observable.
|
---|
43 | * @return {Observable<T>} An Observable that emits the values from the source
|
---|
44 | * Observable until such time as `notifier` emits its first value.
|
---|
45 | * @method takeUntil
|
---|
46 | * @owner Observable
|
---|
47 | */
|
---|
48 | export function takeUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
|
---|
49 | return (source: Observable<T>) => source.lift(new TakeUntilOperator(notifier));
|
---|
50 | }
|
---|
51 |
|
---|
52 | class TakeUntilOperator<T> implements Operator<T, T> {
|
---|
53 | constructor(private notifier: Observable<any>) {
|
---|
54 | }
|
---|
55 |
|
---|
56 | call(subscriber: Subscriber<T>, source: any): TeardownLogic {
|
---|
57 | const takeUntilSubscriber = new TakeUntilSubscriber(subscriber);
|
---|
58 | const notifierSubscription = innerSubscribe(this.notifier, new SimpleInnerSubscriber(takeUntilSubscriber));
|
---|
59 | if (notifierSubscription && !takeUntilSubscriber.seenValue) {
|
---|
60 | takeUntilSubscriber.add(notifierSubscription);
|
---|
61 | return source.subscribe(takeUntilSubscriber);
|
---|
62 | }
|
---|
63 | return takeUntilSubscriber;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * We need this JSDoc comment for affecting ESDoc.
|
---|
69 | * @ignore
|
---|
70 | * @extends {Ignored}
|
---|
71 | */
|
---|
72 | class TakeUntilSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
|
---|
73 | seenValue = false;
|
---|
74 |
|
---|
75 | constructor(destination: Subscriber<any>, ) {
|
---|
76 | super(destination);
|
---|
77 | }
|
---|
78 |
|
---|
79 | notifyNext(): void {
|
---|
80 | this.seenValue = true;
|
---|
81 | this.complete();
|
---|
82 | }
|
---|
83 |
|
---|
84 | notifyComplete(): void {
|
---|
85 | // noop
|
---|
86 | }
|
---|
87 | }
|
---|