95f7f8d4d0cdf88f7a5ce11dd6efdab149bf31a5412c50ebc593d10d1590085ac16e2e3b4743dff24d6bbc45a5d18edd8f952c65298fcaa182fb34daa12c90 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { scanInternals } from './scanInternals';
  2. import { OperatorFunction } from '../types';
  3. import { operate } from '../util/lift';
  4. export function reduce<V, A = V>(accumulator: (acc: A | V, value: V, index: number) => A): OperatorFunction<V, V | A>;
  5. export function reduce<V, A>(accumulator: (acc: A, value: V, index: number) => A, seed: A): OperatorFunction<V, A>;
  6. export function reduce<V, A, S = A>(accumulator: (acc: A | S, value: V, index: number) => A, seed: S): OperatorFunction<V, A>;
  7. /**
  8. * Applies an accumulator function over the source Observable, and returns the
  9. * accumulated result when the source completes, given an optional seed value.
  10. *
  11. * <span class="informal">Combines together all values emitted on the source,
  12. * using an accumulator function that knows how to join a new source value into
  13. * the accumulation from the past.</span>
  14. *
  15. * ![](reduce.png)
  16. *
  17. * Like
  18. * [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce),
  19. * `reduce` applies an `accumulator` function against an accumulation and each
  20. * value of the source Observable (from the past) to reduce it to a single
  21. * value, emitted on the output Observable. Note that `reduce` will only emit
  22. * one value, only when the source Observable completes. It is equivalent to
  23. * applying operator {@link scan} followed by operator {@link last}.
  24. *
  25. * Returns an Observable that applies a specified `accumulator` function to each
  26. * item emitted by the source Observable. If a `seed` value is specified, then
  27. * that value will be used as the initial value for the accumulator. If no seed
  28. * value is specified, the first item of the source is used as the seed.
  29. *
  30. * ## Example
  31. *
  32. * Count the number of click events that happened in 5 seconds
  33. *
  34. * ```ts
  35. * import { fromEvent, takeUntil, interval, map, reduce } from 'rxjs';
  36. *
  37. * const clicksInFiveSeconds = fromEvent(document, 'click')
  38. * .pipe(takeUntil(interval(5000)));
  39. *
  40. * const ones = clicksInFiveSeconds.pipe(map(() => 1));
  41. * const seed = 0;
  42. * const count = ones.pipe(reduce((acc, one) => acc + one, seed));
  43. *
  44. * count.subscribe(x => console.log(x));
  45. * ```
  46. *
  47. * @see {@link count}
  48. * @see {@link expand}
  49. * @see {@link mergeScan}
  50. * @see {@link scan}
  51. *
  52. * @param accumulator The accumulator function called on each source value.
  53. * @param seed The initial accumulation value.
  54. * @return A function that returns an Observable that emits a single value that
  55. * is the result of accumulating the values emitted by the source Observable.
  56. */
  57. export function reduce<V, A>(accumulator: (acc: V | A, value: V, index: number) => A, seed?: any): OperatorFunction<V, V | A> {
  58. return operate(scanInternals(accumulator, seed, arguments.length >= 2, false, true));
  59. }