Skip to content

Function: delayUntil()

delayUntil<T, R>(notifier): Operator<T, T>

Defined in: operators/delayUntil.ts:21

Creates a stream operator that delays the emission of values from the source stream until a separate notifier stream emits at least one value.

This operator acts as a gate. It buffers all values from the source stream until the notifier stream emits its first value. Once the notifier emits, the operator immediately flushes all buffered values and then passes through all subsequent values from the source without delay.

If the notifier stream completes without ever emitting a value, the buffered values are DISCARDED, and the operator simply waits for the source to complete.

Type Parameters

T

T = any

The type of the values in the source and output streams.

R

R = T

Parameters

notifier

The stream that acts as a gatekeeper.

Stream<R> | Promise<R>

Returns

Operator<T, T>

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

Examples

From delayUntil.spec.ts:5

typescript
const sourceStream = createSubject<number>();
const conditionStream = createSubject<any>();
const emittedValues: number[] = [];
const delayedStream = sourceStream.pipe(delayUntil(conditionStream));
await new Promise<void>((resolve, reject) => {
  delayedStream.subscribe({
    next: (value) => emittedValues.push(value),
    complete: () => {
      try {
        expect(emittedValues).toEqual([1, 2, 3, 4]); // Expectation is now waited for
        resolve();
      } catch (e) {
        reject(e);
      }
    },
    error: (err) => reject(new Error(`Stream failed: ${err}`)),
  });

  sourceStream.next(1); // Buffered
  conditionStream.next('start'); // Emission starts
  sourceStream.next(2); // Emitted
  sourceStream.next(3); // Emitted
  sourceStream.next(4); // Emitted

  // FIX: Must complete the source stream for the delayed stream to complete
  sourceStream.complete();
  conditionStream.complete();
});

Released under the MIT License.