739aeb7fb7554c857de37e7a5bdb92229ab301446afd66b7f2db9569ba1acdc921ce09729ea767c1b281e9622763555b178c2d46bac5c3e4f35042ddb6d78b 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 accumulator The accumulator function called on each source value.
  62. * @param seed The initial accumulation value.
  63. * @param concurrent Maximum number of input Observables being subscribed to
  64. * concurrently.
  65. * @return A function that returns an Observable of the accumulated values.
  66. */
  67. export declare function mergeScan<T, R>(accumulator: (acc: R, value: T, index: number) => ObservableInput<R>, seed: R, concurrent?: number): OperatorFunction<T, R>;
  68. //# sourceMappingURL=mergeScan.d.ts.map