|
| 1 | +import { async } from '../scheduler/async'; |
| 2 | +import { IScheduler } from '../Scheduler'; |
| 3 | +import { audit } from './audit'; |
| 4 | +import { timer } from '../observable/timer'; |
| 5 | +import { MonoTypeOperatorFunction } from '../interfaces'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Ignores source values for `duration` milliseconds, then emits the most recent |
| 9 | + * value from the source Observable, then repeats this process. |
| 10 | + * |
| 11 | + * <span class="informal">When it sees a source values, it ignores that plus |
| 12 | + * the next ones for `duration` milliseconds, and then it emits the most recent |
| 13 | + * value from the source.</span> |
| 14 | + * |
| 15 | + * <img src="./img/auditTime.png" width="100%"> |
| 16 | + * |
| 17 | + * `auditTime` is similar to `throttleTime`, but emits the last value from the |
| 18 | + * silenced time window, instead of the first value. `auditTime` emits the most |
| 19 | + * recent value from the source Observable on the output Observable as soon as |
| 20 | + * its internal timer becomes disabled, and ignores source values while the |
| 21 | + * timer is enabled. Initially, the timer is disabled. As soon as the first |
| 22 | + * source value arrives, the timer is enabled. After `duration` milliseconds (or |
| 23 | + * the time unit determined internally by the optional `scheduler`) has passed, |
| 24 | + * the timer is disabled, then the most recent source value is emitted on the |
| 25 | + * output Observable, and this process repeats for the next source value. |
| 26 | + * Optionally takes a {@link IScheduler} for managing timers. |
| 27 | + * |
| 28 | + * @example <caption>Emit clicks at a rate of at most one click per second</caption> |
| 29 | + * var clicks = Rx.Observable.fromEvent(document, 'click'); |
| 30 | + * var result = clicks.auditTime(1000); |
| 31 | + * result.subscribe(x => console.log(x)); |
| 32 | + * |
| 33 | + * @see {@link audit} |
| 34 | + * @see {@link debounceTime} |
| 35 | + * @see {@link delay} |
| 36 | + * @see {@link sampleTime} |
| 37 | + * @see {@link throttleTime} |
| 38 | + * |
| 39 | + * @param {number} duration Time to wait before emitting the most recent source |
| 40 | + * value, measured in milliseconds or the time unit determined internally |
| 41 | + * by the optional `scheduler`. |
| 42 | + * @param {Scheduler} [scheduler=async] The {@link IScheduler} to use for |
| 43 | + * managing the timers that handle the rate-limiting behavior. |
| 44 | + * @return {Observable<T>} An Observable that performs rate-limiting of |
| 45 | + * emissions from the source Observable. |
| 46 | + * @method auditTime |
| 47 | + * @owner Observable |
| 48 | + */ |
| 49 | +export function auditTime<T>(duration: number, scheduler: IScheduler = async): MonoTypeOperatorFunction<T> { |
| 50 | + return audit(() => timer(duration, scheduler)); |
| 51 | +} |
0 commit comments