Debian Codegraph @

node/rxjs/src/internal/operators/auditTime.ts

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