mergeScan.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /** PURE_IMPORTS_START .._util_tryCatch,.._util_errorObject,.._util_subscribeToResult,.._OuterSubscriber 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 { tryCatch } from '../util/tryCatch';
  10. import { errorObject } from '../util/errorObject';
  11. import { subscribeToResult } from '../util/subscribeToResult';
  12. import { OuterSubscriber } from '../OuterSubscriber';
  13. /**
  14. * Applies an accumulator function over the source Observable where the
  15. * accumulator function itself returns an Observable, then each intermediate
  16. * Observable returned is merged into the output Observable.
  17. *
  18. * <span class="informal">It's like {@link scan}, but the Observables returned
  19. * by the accumulator are merged into the outer Observable.</span>
  20. *
  21. * @example <caption>Count the number of click events</caption>
  22. * const click$ = Rx.Observable.fromEvent(document, 'click');
  23. * const one$ = click$.mapTo(1);
  24. * const seed = 0;
  25. * const count$ = one$.mergeScan((acc, one) => Rx.Observable.of(acc + one), seed);
  26. * count$.subscribe(x => console.log(x));
  27. *
  28. * // Results:
  29. * 1
  30. * 2
  31. * 3
  32. * 4
  33. * // ...and so on for each click
  34. *
  35. * @param {function(acc: R, value: T): Observable<R>} accumulator
  36. * The accumulator function called on each source value.
  37. * @param seed The initial accumulation value.
  38. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of
  39. * input Observables being subscribed to concurrently.
  40. * @return {Observable<R>} An observable of the accumulated values.
  41. * @method mergeScan
  42. * @owner Observable
  43. */
  44. export function mergeScan(accumulator, seed, concurrent) {
  45. if (concurrent === void 0) {
  46. concurrent = Number.POSITIVE_INFINITY;
  47. }
  48. return function (source) { return source.lift(new MergeScanOperator(accumulator, seed, concurrent)); };
  49. }
  50. export var MergeScanOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  51. function MergeScanOperator(accumulator, seed, concurrent) {
  52. this.accumulator = accumulator;
  53. this.seed = seed;
  54. this.concurrent = concurrent;
  55. }
  56. MergeScanOperator.prototype.call = function (subscriber, source) {
  57. return source.subscribe(new MergeScanSubscriber(subscriber, this.accumulator, this.seed, this.concurrent));
  58. };
  59. return MergeScanOperator;
  60. }());
  61. /**
  62. * We need this JSDoc comment for affecting ESDoc.
  63. * @ignore
  64. * @extends {Ignored}
  65. */
  66. export var MergeScanSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  67. __extends(MergeScanSubscriber, _super);
  68. function MergeScanSubscriber(destination, accumulator, acc, concurrent) {
  69. _super.call(this, destination);
  70. this.accumulator = accumulator;
  71. this.acc = acc;
  72. this.concurrent = concurrent;
  73. this.hasValue = false;
  74. this.hasCompleted = false;
  75. this.buffer = [];
  76. this.active = 0;
  77. this.index = 0;
  78. }
  79. MergeScanSubscriber.prototype._next = function (value) {
  80. if (this.active < this.concurrent) {
  81. var index = this.index++;
  82. var ish = tryCatch(this.accumulator)(this.acc, value);
  83. var destination = this.destination;
  84. if (ish === errorObject) {
  85. destination.error(errorObject.e);
  86. }
  87. else {
  88. this.active++;
  89. this._innerSub(ish, value, index);
  90. }
  91. }
  92. else {
  93. this.buffer.push(value);
  94. }
  95. };
  96. MergeScanSubscriber.prototype._innerSub = function (ish, value, index) {
  97. this.add(subscribeToResult(this, ish, value, index));
  98. };
  99. MergeScanSubscriber.prototype._complete = function () {
  100. this.hasCompleted = true;
  101. if (this.active === 0 && this.buffer.length === 0) {
  102. if (this.hasValue === false) {
  103. this.destination.next(this.acc);
  104. }
  105. this.destination.complete();
  106. }
  107. };
  108. MergeScanSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  109. var destination = this.destination;
  110. this.acc = innerValue;
  111. this.hasValue = true;
  112. destination.next(innerValue);
  113. };
  114. MergeScanSubscriber.prototype.notifyComplete = function (innerSub) {
  115. var buffer = this.buffer;
  116. this.remove(innerSub);
  117. this.active--;
  118. if (buffer.length > 0) {
  119. this._next(buffer.shift());
  120. }
  121. else if (this.active === 0 && this.hasCompleted) {
  122. if (this.hasValue === false) {
  123. this.destination.next(this.acc);
  124. }
  125. this.destination.complete();
  126. }
  127. };
  128. return MergeScanSubscriber;
  129. }(OuterSubscriber));
  130. //# sourceMappingURL=mergeScan.js.map