Skip to content

combine

Combine two observables with a map function and an optional pre-treatment.

You can use the pre-treatment to add a filter, a distinctUntilChanged, or any other operator that can be used in a pipe, applied to each source before it is combined.

Available since v1.0.1

import { combine } from '@helpers4/observable';
combine<T, U, R>(
  source1: Observable<T>,
  source2: Observable<U>,
  map: (c: readonly [T, U]) => R,
  options?: { preTreatment?: OperatorFunction<readonly [T, U], readonly [T, U]> }
): Observable<R>
ParameterTypeDescription
source1Observable<T>First source of data.
source2Observable<U>Second source of data.
map(c: readonly [T, U]) => RWay to combine the two latest values into the result.
optionscombineOptions<T, U>Optional. preTreatment — an operator (filter, distinctUntilChanged, …) applied to the paired [T, U] values before map runs.

Observable<R> — An observable that emits the result of map every time either source emits.

import { combine } from '@helpers4/observable';
import { interval } from 'rxjs';

const seconds = interval(1000);
const halfSeconds = interval(500);

combine(seconds, halfSeconds, ([s, hs]) => `${s}s / ${hs}half-s`).subscribe(console.log);

View on npm