UNPKG

2.91 kBTypeScriptView Raw
1import { 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 represent the maximum number of inner Observable subscriptions
32 * at a time.
33 *
34 * ## Example
35 * Count the number of click events
36 * ```ts
37 * import { fromEvent, of } from 'rxjs';
38 * import { mapTo, mergeScan } from 'rxjs/operators';
39 *
40 * const click$ = fromEvent(document, 'click');
41 * const one$ = click$.pipe(mapTo(1));
42 * const seed = 0;
43 * const count$ = one$.pipe(
44 * mergeScan((acc, one) => of(acc + one), seed),
45 * );
46 * count$.subscribe(x => console.log(x));
47 *
48 * // Results:
49 * // 1
50 * // 2
51 * // 3
52 * // 4
53 * // ...and so on for each click
54 * ```
55 *
56 * @see {@link scan}
57 * @see {@link switchScan}
58 *
59 * @param {function(acc: R, value: T): Observable<R>} accumulator
60 * The accumulator function called on each source value.
61 * @param seed The initial accumulation value.
62 * @param {number} [concurrent=Infinity] Maximum number of
63 * input Observables being subscribed to concurrently.
64 * @return A function that returns an Observable of the accumulated values.
65 */
66export declare function mergeScan<T, R>(accumulator: (acc: R, value: T, index: number) => ObservableInput<R>, seed: R, concurrent?: number): OperatorFunction<T, R>;
67//# sourceMappingURL=mergeScan.d.ts.map
\No newline at end of file