mergeScan operator allow you merge with other observable and add an accumulator
import { interval, of } from 'rxjs';
import { mapTo, mergeScan } from 'rxjs/operators';
const source$ = interval(1000).pipe(mapTo(1));
const seed = 0;
const count$ = source$.pipe(
mergeScan((acc, val) => of(acc + val), seed),
);
import { fromEvent, of } from 'rxjs';
import { mapTo, mergeScan } from 'rxjs/operators';
const click$ = fromEvent(document, 'click').pipe(mapTo(1));
const seed = 0;
const count$ = click$.pipe(
mergeScan((acc, val) => of(acc + val), seed),
);
Official Doc: rxjs.mergeScan