1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
5 import { OuterSubscriber } from '../OuterSubscriber';
6 import { InnerSubscriber } from '../InnerSubscriber';
7 import { subscribeToResult } from '../util/subscribeToResult';
9 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
12 * Emits the values emitted by the source Observable until a `notifier`
13 * Observable emits a value.
15 * <span class="informal">Lets values pass until a second Observable,
16 * `notifier`, emits a value. Then, it completes.</span>
20 * `takeUntil` subscribes and begins mirroring the source Observable. It also
21 * monitors a second Observable, `notifier` that you provide. If the `notifier`
22 * emits a value, the output Observable stops mirroring the source Observable
23 * and completes. If the `notifier` doesn't emit any value and completes
24 * then `takeUntil` will pass all values.
27 * Tick every second until the first click happens
29 * import { fromEvent, interval } from 'rxjs';
30 * import { takeUntil } from 'rxjs/operators';
32 * const source = interval(1000);
33 * const clicks = fromEvent(document, 'click');
34 * const result = source.pipe(takeUntil(clicks));
35 * result.subscribe(x => console.log(x));
39 * @see {@link takeLast}
40 * @see {@link takeWhile}
43 * @param {Observable} notifier The Observable whose first emitted value will
44 * cause the output Observable of `takeUntil` to stop emitting values from the
46 * @return {Observable<T>} An Observable that emits the values from the source
47 * Observable until such time as `notifier` emits its first value.
51 export function takeUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
52 return (source: Observable<T>) => source.lift(new TakeUntilOperator(notifier));
55 class TakeUntilOperator<T> implements Operator<T, T> {
56 constructor(private notifier: Observable<any>) {
59 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
60 const takeUntilSubscriber = new TakeUntilSubscriber(subscriber);
61 const notifierSubscription = subscribeToResult(takeUntilSubscriber, this.notifier);
62 if (notifierSubscription && !takeUntilSubscriber.seenValue) {
63 takeUntilSubscriber.add(notifierSubscription);
64 return source.subscribe(takeUntilSubscriber);
66 return takeUntilSubscriber;
71 * We need this JSDoc comment for affecting ESDoc.
75 class TakeUntilSubscriber<T, R> extends OuterSubscriber<T, R> {
78 constructor(destination: Subscriber<any>, ) {
82 notifyNext(outerValue: T, innerValue: R,
83 outerIndex: number, innerIndex: number,
84 innerSub: InnerSubscriber<T, R>): void {
85 this.seenValue = true;
89 notifyComplete(): void {