1 | import { ObservableInput, OperatorFunction } from '../types';
|
2 | /**
|
3 | * Applies an accumulator function over the source Observable where the
|
4 | * accumulator function itself returns an Observable, then each intermediate
|
5 | * Observable returned is merged into the output Observable.
|
6 | *
|
7 | * <span class="informal">It's like {@link scan}, but the Observables returned
|
8 | * by the accumulator are merged into the outer Observable.</span>
|
9 | *
|
10 | * The first parameter of the `mergeScan` is an `accumulator` function which is
|
11 | * being called every time the source Observable emits a value. `mergeScan` will
|
12 | * subscribe to the value returned by the `accumulator` function and will emit
|
13 | * values to the subscriber emitted by inner Observable.
|
14 | *
|
15 | * The `accumulator` function is being called with three parameters passed to it:
|
16 | * `acc`, `value` and `index`. The `acc` parameter is used as the state parameter
|
17 | * whose value is initially set to the `seed` parameter (the second parameter
|
18 | * passed to the `mergeScan` operator).
|
19 | *
|
20 | * `mergeScan` internally keeps the value of the `acc` parameter: as long as the
|
21 | * source Observable emits without inner Observable emitting, the `acc` will be
|
22 | * set to `seed`. The next time the inner Observable emits a value, `mergeScan`
|
23 | * will internally remember it and it will be passed to the `accumulator`
|
24 | * function as `acc` parameter the next time source emits.
|
25 | *
|
26 | * The `value` parameter of the `accumulator` function is the value emitted by the
|
27 | * source Observable, while the `index` is a number which represent the order of the
|
28 | * current emission by the source Observable. It starts with 0.
|
29 | *
|
30 | * The last parameter to the `mergeScan` is the `concurrent` value which defaults
|
31 | * to Infinity. It represents the maximum number of inner Observable subscriptions
|
32 | * at a time.
|
33 | *
|
34 | * ## Example
|
35 | *
|
36 | * Count the number of click events
|
37 | *
|
38 | * ```ts
|
39 | * import { fromEvent, map, mergeScan, of } from 'rxjs';
|
40 | *
|
41 | * const click$ = fromEvent(document, 'click');
|
42 | * const one$ = click$.pipe(map(() => 1));
|
43 | * const seed = 0;
|
44 | * const count$ = one$.pipe(
|
45 | * mergeScan((acc, one) => of(acc + one), seed)
|
46 | * );
|
47 | *
|
48 | * count$.subscribe(x => console.log(x));
|
49 | *
|
50 | * // Results:
|
51 | * // 1
|
52 | * // 2
|
53 | * // 3
|
54 | * // 4
|
55 | * // ...and so on for each click
|
56 | * ```
|
57 | *
|
58 | * @see {@link scan}
|
59 | * @see {@link switchScan}
|
60 | *
|
61 | * @param {function(acc: R, value: T): Observable<R>} accumulator
|
62 | * The accumulator function called on each source value.
|
63 | * @param seed The initial accumulation value.
|
64 | * @param {number} [concurrent=Infinity] Maximum number of
|
65 | * input Observables being subscribed to concurrently.
|
66 | * @return A function that returns an Observable of the accumulated values.
|
67 | */
|
68 | export declare function mergeScan<T, R>(accumulator: (acc: R, value: T, index: number) => ObservableInput<R>, seed: R, concurrent?: number): OperatorFunction<T, R>;
|
69 | //# sourceMappingURL=mergeScan.d.ts.map |
\ | No newline at end of file |