1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { OuterSubscriber } from '../OuterSubscriber';
5 import { InnerSubscriber } from '../InnerSubscriber';
6 import { subscribeToResult } from '../util/subscribeToResult';
7 import { MonoTypeOperatorFunction, TeardownLogic, ObservableInput } from '../types';
8 import { Subscription } from '../Subscription';
11 * Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item.
13 * The `skipUntil` operator causes the observable stream to skip the emission of values until the passed in observable emits the first value.
14 * This can be particularly useful in combination with user interactions, responses of http requests or waiting for specific times to pass by.
18 * Internally the `skipUntil` operator subscribes to the passed in observable (in the following called *notifier*) in order to recognize the emission
19 * of its first value. When this happens, the operator unsubscribes from the *notifier* and starts emitting the values of the *source*
20 * observable. It will never let the *source* observable emit any values if the *notifier* completes or throws an error without emitting
25 * In the following example, all emitted values of the interval observable are skipped until the user clicks anywhere within the page.
28 * import { interval, fromEvent } from 'rxjs';
29 * import { skipUntil } from 'rxjs/operators';
31 * const intervalObservable = interval(1000);
32 * const click = fromEvent(document, 'click');
34 * const emitAfterClick = intervalObservable.pipe(
37 * // clicked at 4.6s. output: 5...6...7...8........ or
38 * // clicked at 7.3s. output: 8...9...10..11.......
39 * const subscribe = emitAfterClick.subscribe(value => console.log(value));
42 * @param {Observable} notifier - The second Observable that has to emit an item before the source Observable's elements begin to
43 * be mirrored by the resulting Observable.
44 * @return {Observable<T>} An Observable that skips items from the source Observable until the second Observable emits
45 * an item, then emits the remaining items.
49 export function skipUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
50 return (source: Observable<T>) => source.lift(new SkipUntilOperator(notifier));
53 class SkipUntilOperator<T> implements Operator<T, T> {
54 constructor(private notifier: Observable<any>) {
57 call(destination: Subscriber<T>, source: any): TeardownLogic {
58 return source.subscribe(new SkipUntilSubscriber(destination, this.notifier));
63 * We need this JSDoc comment for affecting ESDoc.
67 class SkipUntilSubscriber<T, R> extends OuterSubscriber<T, R> {
69 private hasValue: boolean = false;
70 private innerSubscription: Subscription;
72 constructor(destination: Subscriber<R>, notifier: ObservableInput<any>) {
74 const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
75 this.add(innerSubscriber);
76 this.innerSubscription = innerSubscriber;
77 const innerSubscription = subscribeToResult(this, notifier, undefined, undefined, innerSubscriber);
78 // The returned subscription will usually be the subscriber that was
79 // passed. However, interop subscribers will be wrapped and for
80 // unsubscriptions to chain correctly, the wrapper needs to be added, too.
81 if (innerSubscription !== innerSubscriber) {
82 this.add(innerSubscription);
83 this.innerSubscription = innerSubscription;
87 protected _next(value: T) {
93 notifyNext(outerValue: T, innerValue: R,
94 outerIndex: number, innerIndex: number,
95 innerSub: InnerSubscriber<T, R>): void {
97 if (this.innerSubscription) {
98 this.innerSubscription.unsubscribe();