reduce.js 2.8 KB

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