Demo:  auditTime

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

It keeps repeating the same process after every emit.

auditTime(auditTimeInMilliseconds)

It is useful for sampling the latest values for changing parameters like :

  • The mouse positions when the user moves on your gaming app.
  • User typed text for our typeahead or autocomplete features.
//Emit with delay equals to num*0.1sec
const source = of(2,8,15,25,4,45,6,2)
            .pipe(concatMap(v=>of(v).pipe(delay(100*v))));

//Each audit time interval,
//emits the latest values at the end of the intervals.
const timeIntervalFilter = auditTime(2000);

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

The "audit Time" is a time-interval filter that waits for the specified audit interval to end: - to emit the latest item from the source.

As we will see in more detail below, this helps in two ways : - One, it allows us to capture the value after a sizable change. - Two, it avoids the loss of the latest values as in case of sampling using "throttle Time".

Let's come back to our demo to verify the features :

  • As soon as 2 arrives, audit time of 2 seconds starts.
    • Since, 8 is the latest item that comes in the next 2 seconds, the filter emits it at the end of the interval.
  • The same cycle repeats again for 15.
    • But since no new value arrives for next 2 seconds, it emits 15 itself for the interval.
  • The cycle repeats again for 25, the audit time starts when 25 arrives.
    • The latest value in next 2 seconds is 4. So, it emits 4.
  • Coming to the last but, the important one, the audit time starts again when 45 arrives.
    • Even if the source completes in next 6 + 2, 8 seconds!
    • Audit time still emits 2, after the specified interval of 2 seconds, internally delaying the complete notification.
  • But, if we would have used the throttleTime operator which is another similar time-interval filter:
    • We would have missed this last value of 2.
More on : auditTime

A close counterpart to auditTime is throttleTime.

  • Whereas throttleTime discards the inputs for a specified interval, after every emit.
  • auditTime instead of emiting the first input, waits for the interval to be over to emits it's latest value.

The difference of emitting at the begining and the end of intervals makes each of them suitable for separate scenarios. For instance,

  • Proactive Sampling, to Rate Limit Similar Inputs

    • throttleTime is suitable to limit the rate of inputs, with no delay at the first emit.
    • It is suitable for inputs such as clicks, which are similar and do not represent a change. In case of changing values, the throttleTime is likely to miss out on the last changed value.
  • Delayed Sampling, to Capture the Changes

    • auditTime is suitable for auto-complete or typeahead cases, where the delayed sampling, at the end of the interval, helps us take a useful change in the user input.

Usages

1. Delayed Sampling, to Capture a Sizable Change.

Let's say we are adding a typeahead or auto-complete feature.

  • Capture a Sizable Change : There is no point taking the first letter as the user starts typing. Hence, a delayed sampling as in auditTime with an interval of 0.5 sec will keep giving us few letters which will be useful for finding our further suggestions.

  • Capture the Latest State : Besides the sizable change, it's also important not to missout the latest changes which may happen with throttleTime but, not with auditTime.

Considering both of the above points, auditTime is the right choice for this use case.

import { interval, fromEvent , auditTime , debounceTime, tap } from 'rxjs';

const searchInputElement = document.getElementById('search-input');
//Emit event on every 'keyup'
const searchKeyUpObservable = fromEvent(searchInputElement, 'keyup')

searchKeyUpObservable.pipe(
    //Emit latest events every 0.5sec, when the user starts typing
    auditTime(500),

    //search for typeahead suggestions
    tap(event => console.log(" Searching for : "+ event.target.value))
).subscribe();