mergeScan

Applies an accumulator function over the source Observable where the accumulator function itself returns an Observable, then each intermediate Observable returned is merged into the output Observable.

mergeScan<T, R>(accumulator: (acc: R, value: T, index: number) => ObservableInput<R>, seed: R, concurrent: number = Number.POSITIVE_INFINITY): OperatorFunction<T, R>

Parameters

accumulator

The accumulator function called on each source value.

seed

The initial accumulation value.

concurrent

Optional. Default is Number.POSITIVE_INFINITY.

Maximum number of input Observables being subscribed to concurrently.

Returns

OperatorFunction<T, R>: An observable of the accumulated values.

Description

It's like scan, but the Observables returned by the accumulator are merged into the outer Observable.

Example

Count the number of click events

import { fromEvent, of } from 'rxjs'; import { mapTo } from 'rxjs/operators'; const click$ = fromEvent(document, 'click'); const one$ = click$.pipe(mapTo(1)); const seed = 0; const count$ = one$.pipe( mergeScan((acc, one) => of(acc + one), seed), ); count$.subscribe(x => console.log(x)); // Results: 1 2 3 4 // ...and so on for each click