mergeScan.js 4.8KB

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