scan.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /** PURE_IMPORTS_START .._Subscriber PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subscriber } from '../Subscriber';
  10. /* tslint:enable:max-line-length */
  11. /**
  12. * Applies an accumulator function over the source Observable, and returns each
  13. * intermediate result, with an optional seed value.
  14. *
  15. * <span class="informal">It's like {@link reduce}, but emits the current
  16. * accumulation whenever the source emits a value.</span>
  17. *
  18. * <img src="./img/scan.png" width="100%">
  19. *
  20. * Combines together all values emitted on the source, using an accumulator
  21. * function that knows how to join a new source value into the accumulation from
  22. * the past. Is similar to {@link reduce}, but emits the intermediate
  23. * accumulations.
  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 <caption>Count the number of click events</caption>
  31. * var clicks = Rx.Observable.fromEvent(document, 'click');
  32. * var ones = clicks.mapTo(1);
  33. * var seed = 0;
  34. * var count = ones.scan((acc, one) => acc + one, seed);
  35. * count.subscribe(x => console.log(x));
  36. *
  37. * @see {@link expand}
  38. * @see {@link mergeScan}
  39. * @see {@link reduce}
  40. *
  41. * @param {function(acc: R, value: T, index: number): R} accumulator
  42. * The accumulator function called on each source value.
  43. * @param {T|R} [seed] The initial accumulation value.
  44. * @return {Observable<R>} An observable of the accumulated values.
  45. * @method scan
  46. * @owner Observable
  47. */
  48. export function scan(accumulator, seed) {
  49. var hasSeed = false;
  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. hasSeed = true;
  57. }
  58. return function scanOperatorFunction(source) {
  59. return source.lift(new ScanOperator(accumulator, seed, hasSeed));
  60. };
  61. }
  62. var ScanOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  63. function ScanOperator(accumulator, seed, hasSeed) {
  64. if (hasSeed === void 0) {
  65. hasSeed = false;
  66. }
  67. this.accumulator = accumulator;
  68. this.seed = seed;
  69. this.hasSeed = hasSeed;
  70. }
  71. ScanOperator.prototype.call = function (subscriber, source) {
  72. return source.subscribe(new ScanSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
  73. };
  74. return ScanOperator;
  75. }());
  76. /**
  77. * We need this JSDoc comment for affecting ESDoc.
  78. * @ignore
  79. * @extends {Ignored}
  80. */
  81. var ScanSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  82. __extends(ScanSubscriber, _super);
  83. function ScanSubscriber(destination, accumulator, _seed, hasSeed) {
  84. _super.call(this, destination);
  85. this.accumulator = accumulator;
  86. this._seed = _seed;
  87. this.hasSeed = hasSeed;
  88. this.index = 0;
  89. }
  90. Object.defineProperty(ScanSubscriber.prototype, "seed", {
  91. get: function () {
  92. return this._seed;
  93. },
  94. set: function (value) {
  95. this.hasSeed = true;
  96. this._seed = value;
  97. },
  98. enumerable: true,
  99. configurable: true
  100. });
  101. ScanSubscriber.prototype._next = function (value) {
  102. if (!this.hasSeed) {
  103. this.seed = value;
  104. this.destination.next(value);
  105. }
  106. else {
  107. return this._tryNext(value);
  108. }
  109. };
  110. ScanSubscriber.prototype._tryNext = function (value) {
  111. var index = this.index++;
  112. var result;
  113. try {
  114. result = this.accumulator(this.seed, value, index);
  115. }
  116. catch (err) {
  117. this.destination.error(err);
  118. }
  119. this.seed = result;
  120. this.destination.next(result);
  121. };
  122. return ScanSubscriber;
  123. }(Subscriber));
  124. //# sourceMappingURL=scan.js.map