Demo:  audit

The audit is a time-interval filter that waits for the specified inner observable to emit a value, to emit the latest item from the source.

The audit is a time-interval filter, very similar to auditTime.

Instead of the time in milliseconds as in auditTime, audit uses the specified innerObservable to decide on the time interval.

audit(v => innerObservable)
//Emit with delay equals to num*0.1sec
const source = of(2,5,6,15,5,25,6,6,18)
         .pipe(concatMap(v=>of(v).pipe(delay(100*v))));

//Each audit interval,
//emits the latest value from source as the innerObservable emits
const timeIntervalFilter = audit(v => timer(2000));

const output = source.pipe(timeIntervalFilter)
                     .subscribe(v=>console.log(v))
What is happening ?

The audit is a time-interval filter, very similar to auditTime.

Instead of the time in milliseconds as in auditTime: here each audit time interval starts by starting the innerObservable, and ends when the innerObservable emits.

Let's understand how it works with the demo.

Let's start with the first item 2 from the source.

  • When the source item 2 arrives the filter, the filter starts the innerObservable, the 2 second timer here.
  • As soon as the innerObservable fires after 2 seconds, filter emits the latest item available.
  • Hence, here the filter emits 6 as it happens to be the latest value as the timer fires. Again, the same cycle starts for 15, where the filter emits 5 as the latest value when the timer fires. Even the last 2 items in the output are also the repeatation of the same cycle which started at 25 and 18 respectively. As we will see the usages under the "audit Time" post:
  • this kind of delayed sampling is very useful in capturing the latest states of a changing value, such as:
  • the user typed text in a searchbox for implementing the typeahead or auto-complete features.
  • or for capturing the latest mouse positions as the user moves through our gaming app.