scan.js 1.9 KB

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