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