|
| 1 | +import { Operator } from '../Operator'; |
| 2 | +import { Observable } from '../Observable'; |
| 3 | +import { Subscriber } from '../Subscriber'; |
| 4 | +import { TeardownLogic } from '../Subscription'; |
| 5 | +import { OuterSubscriber } from '../OuterSubscriber'; |
| 6 | +import { InnerSubscriber } from '../InnerSubscriber'; |
| 7 | +import { subscribeToResult } from '../util/subscribeToResult'; |
| 8 | + |
| 9 | +import { MonoTypeOperatorFunction } from '../interfaces'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Emits the most recently emitted value from the source Observable whenever |
| 13 | + * another Observable, the `notifier`, emits. |
| 14 | + * |
| 15 | + * <span class="informal">It's like {@link sampleTime}, but samples whenever |
| 16 | + * the `notifier` Observable emits something.</span> |
| 17 | + * |
| 18 | + * <img src="./img/sample.png" width="100%"> |
| 19 | + * |
| 20 | + * Whenever the `notifier` Observable emits a value or completes, `sample` |
| 21 | + * looks at the source Observable and emits whichever value it has most recently |
| 22 | + * emitted since the previous sampling, unless the source has not emitted |
| 23 | + * anything since the previous sampling. The `notifier` is subscribed to as soon |
| 24 | + * as the output Observable is subscribed. |
| 25 | + * |
| 26 | + * @example <caption>On every click, sample the most recent "seconds" timer</caption> |
| 27 | + * var seconds = Rx.Observable.interval(1000); |
| 28 | + * var clicks = Rx.Observable.fromEvent(document, 'click'); |
| 29 | + * var result = seconds.sample(clicks); |
| 30 | + * result.subscribe(x => console.log(x)); |
| 31 | + * |
| 32 | + * @see {@link audit} |
| 33 | + * @see {@link debounce} |
| 34 | + * @see {@link sampleTime} |
| 35 | + * @see {@link throttle} |
| 36 | + * |
| 37 | + * @param {Observable<any>} notifier The Observable to use for sampling the |
| 38 | + * source Observable. |
| 39 | + * @return {Observable<T>} An Observable that emits the results of sampling the |
| 40 | + * values emitted by the source Observable whenever the notifier Observable |
| 41 | + * emits value or completes. |
| 42 | + * @method sample |
| 43 | + * @owner Observable |
| 44 | + */ |
| 45 | +export function sample<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> { |
| 46 | + return (source: Observable<T>) => source.lift(new SampleOperator(notifier)); |
| 47 | +} |
| 48 | + |
| 49 | +class SampleOperator<T> implements Operator<T, T> { |
| 50 | + constructor(private notifier: Observable<any>) { |
| 51 | + } |
| 52 | + |
| 53 | + call(subscriber: Subscriber<T>, source: any): TeardownLogic { |
| 54 | + const sampleSubscriber = new SampleSubscriber(subscriber); |
| 55 | + const subscription = source.subscribe(sampleSubscriber); |
| 56 | + subscription.add(subscribeToResult(sampleSubscriber, this.notifier)); |
| 57 | + return subscription; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * We need this JSDoc comment for affecting ESDoc. |
| 63 | + * @ignore |
| 64 | + * @extends {Ignored} |
| 65 | + */ |
| 66 | +class SampleSubscriber<T, R> extends OuterSubscriber<T, R> { |
| 67 | + private value: T; |
| 68 | + private hasValue: boolean = false; |
| 69 | + |
| 70 | + protected _next(value: T) { |
| 71 | + this.value = value; |
| 72 | + this.hasValue = true; |
| 73 | + } |
| 74 | + |
| 75 | + notifyNext(outerValue: T, innerValue: R, |
| 76 | + outerIndex: number, innerIndex: number, |
| 77 | + innerSub: InnerSubscriber<T, R>): void { |
| 78 | + this.emitValue(); |
| 79 | + } |
| 80 | + |
| 81 | + notifyComplete(): void { |
| 82 | + this.emitValue(); |
| 83 | + } |
| 84 | + |
| 85 | + emitValue() { |
| 86 | + if (this.hasValue) { |
| 87 | + this.hasValue = false; |
| 88 | + this.destination.next(this.value); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments