0288544339fdae23ed2e0499b9c4ede56e2ad063e6bbad12b860fedd4f31ad1922327bb2422ff4765c8695bcd05d6d01482b2bf233d20980c7722651fa8d7d 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Observable } from '../Observable';
  2. import { scan } from './scan';
  3. import { takeLast } from './takeLast';
  4. import { defaultIfEmpty } from './defaultIfEmpty';
  5. import { OperatorFunction, MonoTypeOperatorFunction } from '../types';
  6. import { pipe } from '../util/pipe';
  7. /* tslint:disable:max-line-length */
  8. export function reduce<T, R>(accumulator: (acc: R, value: T, index: number) => R, seed: R): OperatorFunction<T, R>;
  9. export function reduce<T>(accumulator: (acc: T, value: T, index: number) => T, seed?: T): MonoTypeOperatorFunction<T>;
  10. export function reduce<T, R>(accumulator: (acc: R, value: T, index: number) => R): OperatorFunction<T, R>;
  11. /* tslint:enable:max-line-length */
  12. /**
  13. * Applies an accumulator function over the source Observable, and returns the
  14. * accumulated result when the source completes, given an optional seed value.
  15. *
  16. * <span class="informal">Combines together all values emitted on the source,
  17. * using an accumulator function that knows how to join a new source value into
  18. * the accumulation from the past.</span>
  19. *
  20. * ![](reduce.png)
  21. *
  22. * Like
  23. * [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce),
  24. * `reduce` applies an `accumulator` function against an accumulation and each
  25. * value of the source Observable (from the past) to reduce it to a single
  26. * value, emitted on the output Observable. Note that `reduce` will only emit
  27. * one value, only when the source Observable completes. It is equivalent to
  28. * applying operator {@link scan} followed by operator {@link last}.
  29. *
  30. * Returns an Observable that applies a specified `accumulator` function to each
  31. * item emitted by the source Observable. If a `seed` value is specified, then
  32. * that value will be used as the initial value for the accumulator. If no seed
  33. * value is specified, the first item of the source is used as the seed.
  34. *
  35. * ## Example
  36. * Count the number of click events that happened in 5 seconds
  37. * ```ts
  38. * import { fromEvent, interval } from 'rxjs';
  39. * import { reduce, takeUntil, mapTo } from 'rxjs/operators';
  40. *
  41. * const clicksInFiveSeconds = fromEvent(document, 'click').pipe(
  42. * takeUntil(interval(5000)),
  43. * );
  44. * const ones = clicksInFiveSeconds.pipe(mapTo(1));
  45. * const seed = 0;
  46. * const count = ones.pipe(reduce((acc, one) => acc + one, seed));
  47. * count.subscribe(x => console.log(x));
  48. * ```
  49. *
  50. * @see {@link count}
  51. * @see {@link expand}
  52. * @see {@link mergeScan}
  53. * @see {@link scan}
  54. *
  55. * @param {function(acc: R, value: T, index: number): R} accumulator The accumulator function
  56. * called on each source value.
  57. * @param {R} [seed] The initial accumulation value.
  58. * @return {Observable<R>} An Observable that emits a single value that is the
  59. * result of accumulating the values emitted by the source Observable.
  60. * @method reduce
  61. * @owner Observable
  62. */
  63. export function reduce<T, R>(accumulator: (acc: T | R, value: T, index?: number) => T | R, seed?: T | R): OperatorFunction<T, T | R> {
  64. // providing a seed of `undefined` *should* be valid and trigger
  65. // hasSeed! so don't use `seed !== undefined` checks!
  66. // For this reason, we have to check it here at the original call site
  67. // otherwise inside Operator/Subscriber we won't know if `undefined`
  68. // means they didn't provide anything or if they literally provided `undefined`
  69. if (arguments.length >= 2) {
  70. return function reduceOperatorFunctionWithSeed(source: Observable<T>): Observable<T | R> {
  71. return pipe(scan(accumulator, seed), takeLast(1), defaultIfEmpty(seed))(source);
  72. };
  73. }
  74. return function reduceOperatorFunction(source: Observable<T>): Observable<T | R> {
  75. return pipe(
  76. scan<T, T | R>((acc, value, index) => accumulator(acc, value, index + 1)),
  77. takeLast(1),
  78. )(source);
  79. };
  80. }