Skip to content

Function: withLatestFrom()

withLatestFrom<T, R>(...streams): Operator<T, [T, ...R[]]>

Defined in: operators/withLatestFrom.ts:32

Creates a stream operator that combines the source stream with the latest values from other provided streams.

This operator is useful for merging a "trigger" stream with "state" streams. It waits for a value from the source stream and, when one arrives, it emits a tuple containing that source value along with the most recently emitted value from each of the other streams.

The operator is "gated" and will not emit any values until all provided streams have emitted at least one value.

Type Parameters

T

T = any

The type of the values in the source stream.

R

R extends readonly unknown[] = any[]

The type of the values in the other streams.

Parameters

streams

...{ [K in string | number | symbol]: Stream<R[K<K>]> | Promise<R[K<K>]> | R[K<K>][] }

An array of streams to combine with the source stream.

Returns

Operator<T, [T, ...R[]]>

An Operator instance that can be used in a stream's pipe method. The output stream emits tuples of [T, ...R].

Examples

From withLatestFrom.spec.ts:4

typescript
const mainStream = from([1, 2, 3]);
const otherStream = from(['A', 'B', 'C']);
const combinedStream = mainStream.pipe(withLatestFrom(otherStream));
let results: any[] = [];
const subscription = combinedStream.subscribe({
  next: (value) => results.push(value),
  error: done.fail,
  complete: () => {
    expect(results).toEqual([]);
    done();
  },
});
subscription.unsubscribe();

Released under the MIT License.