Debian Codegraph @

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

raw
1 import { Subscriber } from '../Subscriber';
2 import { MonoTypeOperatorFunction, ObservableInput } from '../types';
3
4 import { operate } from '../util/lift';
5 import { innerFrom } from '../observable/innerFrom';
6 import { createOperatorSubscriber } from './OperatorSubscriber';
7
8 /**
9 * Ignores source values for a duration determined by another Observable, then
10 * emits the most recent value from the source Observable, then repeats this
11 * process.
12 *
13 * <span class="informal">It's like {@link auditTime}, but the silencing
14 * duration is determined by a second Observable.</span>
15 *
16 * ![](audit.svg)
17 *
18 * `audit` is similar to `throttle`, but emits the last value from the silenced
19 * time window, instead of the first value. `audit` emits the most recent value
20 * from the source Observable on the output Observable as soon as its internal
21 * timer becomes disabled, and ignores source values while the timer is enabled.
22 * Initially, the timer is disabled. As soon as the first source value arrives,
23 * the timer is enabled by calling the `durationSelector` function with the
24 * source value, which returns the "duration" Observable. When the duration
25 * Observable emits a value, the timer is disabled, then the most
26 * recent source value is emitted on the output Observable, and this process
27 * repeats for the next source value.
28 *
29 * ## Example
30 *
31 * Emit clicks at a rate of at most one click per second
32 *
33 * ```ts
34 * import { fromEvent, audit, interval } from 'rxjs';
35 *
36 * const clicks = fromEvent(document, 'click');
37 * const result = clicks.pipe(audit(ev => interval(1000)));
38 * result.subscribe(x => console.log(x));
39 * ```
40 *
41 * @see {@link auditTime}
42 * @see {@link debounce}
43 * @see {@link delayWhen}
44 * @see {@link sample}
45 * @see {@link throttle}
46 *
47 * @param durationSelector A function
48 * that receives a value from the source Observable, for computing the silencing
49 * duration, returned as an Observable or a Promise.
50 * @return A function that returns an Observable that performs rate-limiting of
51 * emissions from the source Observable.
52 */
53 export function audit<T>(durationSelector: (value: T) => ObservableInput<any>): MonoTypeOperatorFunction<T> {
54 return operate((source, subscriber) => {
55 let hasValue = false;
56 let lastValue: T | null = null;
57 let durationSubscriber: Subscriber<any> | null = null;
58 let isComplete = false;
59
60 const endDuration = () => {
61 durationSubscriber?.unsubscribe();
62 durationSubscriber = null;
63 if (hasValue) {
64 hasValue = false;
65 const value = lastValue!;
66 lastValue = null;
67 subscriber.next(value);
68 }
69 isComplete && subscriber.complete();
70 };
71
72 const cleanupDuration = () => {
73 durationSubscriber = null;
74 isComplete && subscriber.complete();
75 };
76
77 source.subscribe(
78 createOperatorSubscriber(
79 subscriber,
80 (value) => {
81 hasValue = true;
82 lastValue = value;
83 if (!durationSubscriber) {
84 innerFrom(durationSelector(value)).subscribe(
85 (durationSubscriber = createOperatorSubscriber(subscriber, endDuration, cleanupDuration))
86 );
87 }
88 },
89 () => {
90 isComplete = true;
91 (!hasValue || !durationSubscriber || durationSubscriber.closed) && subscriber.complete();
92 }
93 )
94 );
95 });
96 }