123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. var scan_1 = require('../operators/scan');
  3. /* tslint:enable:max-line-length */
  4. /**
  5. * Applies an accumulator function over the source Observable, and returns each
  6. * intermediate result, with an optional seed value.
  7. *
  8. * <span class="informal">It's like {@link reduce}, but emits the current
  9. * accumulation whenever the source emits a value.</span>
  10. *
  11. * <img src="./img/scan.png" width="100%">
  12. *
  13. * Combines together all values emitted on the source, using an accumulator
  14. * function that knows how to join a new source value into the accumulation from
  15. * the past. Is similar to {@link reduce}, but emits the intermediate
  16. * accumulations.
  17. *
  18. * Returns an Observable that applies a specified `accumulator` function to each
  19. * item emitted by the source Observable. If a `seed` value is specified, then
  20. * that value will be used as the initial value for the accumulator. If no seed
  21. * value is specified, the first item of the source is used as the seed.
  22. *
  23. * @example <caption>Count the number of click events</caption>
  24. * var clicks = Rx.Observable.fromEvent(document, 'click');
  25. * var ones = clicks.mapTo(1);
  26. * var seed = 0;
  27. * var count = ones.scan((acc, one) => acc + one, seed);
  28. * count.subscribe(x => console.log(x));
  29. *
  30. * @see {@link expand}
  31. * @see {@link mergeScan}
  32. * @see {@link reduce}
  33. *
  34. * @param {function(acc: R, value: T, index: number): R} accumulator
  35. * The accumulator function called on each source value.
  36. * @param {T|R} [seed] The initial accumulation value.
  37. * @return {Observable<R>} An observable of the accumulated values.
  38. * @method scan
  39. * @owner Observable
  40. */
  41. function scan(accumulator, seed) {
  42. if (arguments.length >= 2) {
  43. return scan_1.scan(accumulator, seed)(this);
  44. }
  45. return scan_1.scan(accumulator)(this);
  46. }
  47. exports.scan = scan;
  48. //# sourceMappingURL=scan.js.map