|
| 1 | +import { Operator } from '../Operator'; |
| 2 | +import { Subscriber } from '../Subscriber'; |
| 3 | +import { Observable } from '../Observable'; |
| 4 | +import { TeardownLogic } from '../Subscription'; |
| 5 | +import { OuterSubscriber } from '../OuterSubscriber'; |
| 6 | +import { InnerSubscriber } from '../InnerSubscriber'; |
| 7 | +import { subscribeToResult } from '../util/subscribeToResult'; |
| 8 | +import { MonoTypeOperatorFunction } from '../interfaces'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Returns an Observable that skips items emitted by the source Observable until a second Observable emits an item. |
| 12 | + * |
| 13 | + * <img src="./img/skipUntil.png" width="100%"> |
| 14 | + * |
| 15 | + * @param {Observable} notifier - The second Observable that has to emit an item before the source Observable's elements begin to |
| 16 | + * be mirrored by the resulting Observable. |
| 17 | + * @return {Observable<T>} An Observable that skips items from the source Observable until the second Observable emits |
| 18 | + * an item, then emits the remaining items. |
| 19 | + * @method skipUntil |
| 20 | + * @owner Observable |
| 21 | + */ |
| 22 | +export function skipUntil<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> { |
| 23 | + return (source: Observable<T>) => source.lift(new SkipUntilOperator(notifier)); |
| 24 | +} |
| 25 | + |
| 26 | +class SkipUntilOperator<T> implements Operator<T, T> { |
| 27 | + constructor(private notifier: Observable<any>) { |
| 28 | + } |
| 29 | + |
| 30 | + call(subscriber: Subscriber<T>, source: any): TeardownLogic { |
| 31 | + return source.subscribe(new SkipUntilSubscriber(subscriber, this.notifier)); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * We need this JSDoc comment for affecting ESDoc. |
| 37 | + * @ignore |
| 38 | + * @extends {Ignored} |
| 39 | + */ |
| 40 | +class SkipUntilSubscriber<T, R> extends OuterSubscriber<T, R> { |
| 41 | + |
| 42 | + private hasValue: boolean = false; |
| 43 | + private isInnerStopped: boolean = false; |
| 44 | + |
| 45 | + constructor(destination: Subscriber<any>, |
| 46 | + notifier: Observable<any>) { |
| 47 | + super(destination); |
| 48 | + this.add(subscribeToResult(this, notifier)); |
| 49 | + } |
| 50 | + |
| 51 | + protected _next(value: T) { |
| 52 | + if (this.hasValue) { |
| 53 | + super._next(value); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + protected _complete() { |
| 58 | + if (this.isInnerStopped) { |
| 59 | + super._complete(); |
| 60 | + } else { |
| 61 | + this.unsubscribe(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + notifyNext(outerValue: T, innerValue: R, |
| 66 | + outerIndex: number, innerIndex: number, |
| 67 | + innerSub: InnerSubscriber<T, R>): void { |
| 68 | + this.hasValue = true; |
| 69 | + } |
| 70 | + |
| 71 | + notifyComplete(): void { |
| 72 | + this.isInnerStopped = true; |
| 73 | + if (this.isStopped) { |
| 74 | + super._complete(); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments