Skip to content

Function: audit()

audit<T>(duration): Operator<T, T>

Defined in: operators/audit.ts:17

Creates a stream operator that emits the latest value from the source stream at most once per specified duration, while managing pending and phantom states.

Every value is added to the PipeContext.pendingResults set. If a new value arrives while the timer is active, the previous value is marked as phantom and removed from pending. The last value is resolved when emitted downstream or upon completion.

Type Parameters

T

T = any

The type of the values in the stream.

Parameters

duration

number

The time in milliseconds to wait before emitting the latest value.

Returns

Operator<T, T>

An Operator instance that can be used in a stream's pipe method.

Examples

From audit.spec.ts:10

typescript
let input: ReturnType<typeof createSubject<number>> = createSubject<number>();

const auditedStream = input.pipe(audit(100));
const receivedValues: number[] = [];
auditedStream.subscribe({
  next: (value: any) => receivedValues.push(value),
  complete: () => {
    // The last buffered value is always emitted on completion.
    expect(receivedValues).toEqual([2, 4, 5]);
    done();
  },
});
input.next(1);
setTimeout(() => input.next(2), 50);
setTimeout(() => input.next(3), 150);
setTimeout(() => input.next(4), 200);
setTimeout(() => input.next(5), 300);
setTimeout(() => input.complete(), 400);

Released under the MIT License.