Ignores source values for a duration determined by another Observable, then emits the most recent value from the source Observable, then repeats this process.
audit is similar to throttle, but emits the last value from the silenced time window, instead of the first value. This makes it useful when you want to capture the final state after a burst of activity.
A function that receives a value from the source Observable, for computing the silencing duration, returned as an Observable or a Promise.The function is called with each source value when the internal timer is disabled. The returned Observable determines how long to wait before emitting the most recent value.
When the first source value arrives, the timer is enabled by calling durationSelector(value)
While the timer is enabled, all subsequent source values are ignored (but the most recent is cached)
When the duration Observable emits, the timer is disabled and the most recent cached value is emitted
This process repeats for the next source value
Use audit when you want to capture the final state after rapid changes, such as the final scroll position after scrolling, or the last search term after rapid typing.
import { fromEvent, audit, interval } from 'rxjs';const clicks = fromEvent(document, 'click');const result = clicks.pipe( audit(ev => interval(1000)));result.subscribe(x => console.log(x));// Emits click events at most once per second, emitting the last click in each window
The durationSelector function should return an Observable that completes or emits at least one value. If it never emits, the cached value will never be released.