1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
5 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
6 import { innerSubscribe, SimpleInnerSubscriber, SimpleOuterSubscriber } from '../innerSubscribe';
9 * Emits the values emitted by the source Observable until a `notifier`
10 * Observable emits a value.
12 * <span class="informal">Lets values pass until a second Observable,
13 * `notifier`, emits a value. Then, it completes.</span>
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.
24 * Tick every second until the first click happens
26 * import { fromEvent, interval } from 'rxjs';
27 * import { takeUntil } from 'rxjs/operators';
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));
36 * @see {@link takeLast}
37 * @see {@link takeWhile}
40 * @param {Observable} notifier The Observable whose first emitted value will
41 * cause the output Observable of `takeUntil` to stop emitting values from the
43 * @return {Observable<T>} An Observable that emits the values from the source
44 * Observable until such time as `notifier` emits its first value.
48 export function takeUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
49 return (source: Observable<T>) => source.lift(new TakeUntilOperator(notifier));
52 class TakeUntilOperator<T> implements Operator<T, T> {
53 constructor(private notifier: Observable<any>) {
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);
63 return takeUntilSubscriber;
68 * We need this JSDoc comment for affecting ESDoc.
72 class TakeUntilSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
75 constructor(destination: Subscriber<any>, ) {
80 this.seenValue = true;
84 notifyComplete(): void {