|
| 1 | +import { Observable } from '../Observable'; |
| 2 | +import { Operator } from '../Operator'; |
| 3 | +import { Subscriber } from '../Subscriber'; |
| 4 | +import { IScheduler } from '../Scheduler'; |
| 5 | +import { Action } from '../scheduler/Action'; |
| 6 | +import { async } from '../scheduler/async'; |
| 7 | +import { TeardownLogic } from '../Subscription'; |
| 8 | + |
| 9 | +import { MonoTypeOperatorFunction } from '../interfaces'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Emits the most recently emitted value from the source Observable within |
| 13 | + * periodic time intervals. |
| 14 | + * |
| 15 | + * <span class="informal">Samples the source Observable at periodic time |
| 16 | + * intervals, emitting what it samples.</span> |
| 17 | + * |
| 18 | + * <img src="./img/sampleTime.png" width="100%"> |
| 19 | + * |
| 20 | + * `sampleTime` periodically looks at the source Observable and emits whichever |
| 21 | + * value it has most recently emitted since the previous sampling, unless the |
| 22 | + * source has not emitted anything since the previous sampling. The sampling |
| 23 | + * happens periodically in time every `period` milliseconds (or the time unit |
| 24 | + * defined by the optional `scheduler` argument). The sampling starts as soon as |
| 25 | + * the output Observable is subscribed. |
| 26 | + * |
| 27 | + * @example <caption>Every second, emit the most recent click at most once</caption> |
| 28 | + * var clicks = Rx.Observable.fromEvent(document, 'click'); |
| 29 | + * var result = clicks.sampleTime(1000); |
| 30 | + * result.subscribe(x => console.log(x)); |
| 31 | + * |
| 32 | + * @see {@link auditTime} |
| 33 | + * @see {@link debounceTime} |
| 34 | + * @see {@link delay} |
| 35 | + * @see {@link sample} |
| 36 | + * @see {@link throttleTime} |
| 37 | + * |
| 38 | + * @param {number} period The sampling period expressed in milliseconds or the |
| 39 | + * time unit determined internally by the optional `scheduler`. |
| 40 | + * @param {Scheduler} [scheduler=async] The {@link IScheduler} to use for |
| 41 | + * managing the timers that handle the sampling. |
| 42 | + * @return {Observable<T>} An Observable that emits the results of sampling the |
| 43 | + * values emitted by the source Observable at the specified time interval. |
| 44 | + * @method sampleTime |
| 45 | + * @owner Observable |
| 46 | + */ |
| 47 | +export function sampleTime<T>(period: number, scheduler: IScheduler = async): MonoTypeOperatorFunction<T> { |
| 48 | + return (source: Observable<T>) => source.lift(new SampleTimeOperator(period, scheduler)); |
| 49 | +} |
| 50 | + |
| 51 | +class SampleTimeOperator<T> implements Operator<T, T> { |
| 52 | + constructor(private period: number, |
| 53 | + private scheduler: IScheduler) { |
| 54 | + } |
| 55 | + |
| 56 | + call(subscriber: Subscriber<T>, source: any): TeardownLogic { |
| 57 | + return source.subscribe(new SampleTimeSubscriber(subscriber, this.period, this.scheduler)); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * We need this JSDoc comment for affecting ESDoc. |
| 63 | + * @ignore |
| 64 | + * @extends {Ignored} |
| 65 | + */ |
| 66 | +class SampleTimeSubscriber<T> extends Subscriber<T> { |
| 67 | + lastValue: T; |
| 68 | + hasValue: boolean = false; |
| 69 | + |
| 70 | + constructor(destination: Subscriber<T>, |
| 71 | + private period: number, |
| 72 | + private scheduler: IScheduler) { |
| 73 | + super(destination); |
| 74 | + this.add(scheduler.schedule(dispatchNotification, period, { subscriber: this, period })); |
| 75 | + } |
| 76 | + |
| 77 | + protected _next(value: T) { |
| 78 | + this.lastValue = value; |
| 79 | + this.hasValue = true; |
| 80 | + } |
| 81 | + |
| 82 | + notifyNext() { |
| 83 | + if (this.hasValue) { |
| 84 | + this.hasValue = false; |
| 85 | + this.destination.next(this.lastValue); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function dispatchNotification<T>(this: Action<any>, state: any) { |
| 91 | + let { subscriber, period } = state; |
| 92 | + subscriber.notifyNext(); |
| 93 | + this.schedule(state, period); |
| 94 | +} |
0 commit comments