Debian Codegraph @

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

raw
1 import { asyncScheduler } from '../scheduler/async';
2 import { MonoTypeOperatorFunction, SchedulerLike } from '../types';
3 import { sample } from './sample';
4 import { interval } from '../observable/interval';
5
6 /**
7 * Emits the most recently emitted value from the source Observable within
8 * periodic time intervals.
9 *
10 * <span class="informal">Samples the source Observable at periodic time
11 * intervals, emitting what it samples.</span>
12 *
13 * ![](sampleTime.png)
14 *
15 * `sampleTime` periodically looks at the source Observable and emits whichever
16 * value it has most recently emitted since the previous sampling, unless the
17 * source has not emitted anything since the previous sampling. The sampling
18 * happens periodically in time every `period` milliseconds (or the time unit
19 * defined by the optional `scheduler` argument). The sampling starts as soon as
20 * the output Observable is subscribed.
21 *
22 * ## Example
23 *
24 * Every second, emit the most recent click at most once
25 *
26 * ```ts
27 * import { fromEvent, sampleTime } from 'rxjs';
28 *
29 * const clicks = fromEvent(document, 'click');
30 * const result = clicks.pipe(sampleTime(1000));
31 *
32 * result.subscribe(x => console.log(x));
33 * ```
34 *
35 * @see {@link auditTime}
36 * @see {@link debounceTime}
37 * @see {@link delay}
38 * @see {@link sample}
39 * @see {@link throttleTime}
40 *
41 * @param period The sampling period expressed in milliseconds or the time unit
42 * determined internally by the optional `scheduler`.
43 * @param scheduler The {@link SchedulerLike} to use for managing the timers
44 * that handle the sampling.
45 * @return A function that returns an Observable that emits the results of
46 * sampling the values emitted by the source Observable at the specified time
47 * interval.
48 */
49 export function sampleTime<T>(period: number, scheduler: SchedulerLike = asyncScheduler): MonoTypeOperatorFunction<T> {
50 return sample(interval(period, scheduler));
51 }