mergeMapTo.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 OuterSubscriber_1 = require('../OuterSubscriber');
  8. var subscribeToResult_1 = require('../util/subscribeToResult');
  9. /* tslint:enable:max-line-length */
  10. /**
  11. * Projects each source value to the same Observable which is merged multiple
  12. * times in the output Observable.
  13. *
  14. * <span class="informal">It's like {@link mergeMap}, but maps each value always
  15. * to the same inner Observable.</span>
  16. *
  17. * <img src="./img/mergeMapTo.png" width="100%">
  18. *
  19. * Maps each source value to the given Observable `innerObservable` regardless
  20. * of the source value, and then merges those resulting Observables into one
  21. * single Observable, which is the output Observable.
  22. *
  23. * @example <caption>For each click event, start an interval Observable ticking every 1 second</caption>
  24. * var clicks = Rx.Observable.fromEvent(document, 'click');
  25. * var result = clicks.mergeMapTo(Rx.Observable.interval(1000));
  26. * result.subscribe(x => console.log(x));
  27. *
  28. * @see {@link concatMapTo}
  29. * @see {@link merge}
  30. * @see {@link mergeAll}
  31. * @see {@link mergeMap}
  32. * @see {@link mergeScan}
  33. * @see {@link switchMapTo}
  34. *
  35. * @param {ObservableInput} innerObservable An Observable to replace each value from
  36. * the source Observable.
  37. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  38. * A function to produce the value on the output Observable based on the values
  39. * and the indices of the source (outer) emission and the inner Observable
  40. * emission. The arguments passed to this function are:
  41. * - `outerValue`: the value that came from the source
  42. * - `innerValue`: the value that came from the projected Observable
  43. * - `outerIndex`: the "index" of the value that came from the source
  44. * - `innerIndex`: the "index" of the value from the projected Observable
  45. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
  46. * Observables being subscribed to concurrently.
  47. * @return {Observable} An Observable that emits items from the given
  48. * `innerObservable` (and optionally transformed through `resultSelector`) every
  49. * time a value is emitted on the source Observable.
  50. * @method mergeMapTo
  51. * @owner Observable
  52. */
  53. function mergeMapTo(innerObservable, resultSelector, concurrent) {
  54. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  55. if (typeof resultSelector === 'number') {
  56. concurrent = resultSelector;
  57. resultSelector = null;
  58. }
  59. return function (source) { return source.lift(new MergeMapToOperator(innerObservable, resultSelector, concurrent)); };
  60. }
  61. exports.mergeMapTo = mergeMapTo;
  62. // TODO: Figure out correct signature here: an Operator<Observable<T>, R>
  63. // needs to implement call(observer: Subscriber<R>): Subscriber<Observable<T>>
  64. var MergeMapToOperator = (function () {
  65. function MergeMapToOperator(ish, resultSelector, concurrent) {
  66. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  67. this.ish = ish;
  68. this.resultSelector = resultSelector;
  69. this.concurrent = concurrent;
  70. }
  71. MergeMapToOperator.prototype.call = function (observer, source) {
  72. return source.subscribe(new MergeMapToSubscriber(observer, this.ish, this.resultSelector, this.concurrent));
  73. };
  74. return MergeMapToOperator;
  75. }());
  76. exports.MergeMapToOperator = MergeMapToOperator;
  77. /**
  78. * We need this JSDoc comment for affecting ESDoc.
  79. * @ignore
  80. * @extends {Ignored}
  81. */
  82. var MergeMapToSubscriber = (function (_super) {
  83. __extends(MergeMapToSubscriber, _super);
  84. function MergeMapToSubscriber(destination, ish, resultSelector, concurrent) {
  85. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  86. _super.call(this, destination);
  87. this.ish = ish;
  88. this.resultSelector = resultSelector;
  89. this.concurrent = concurrent;
  90. this.hasCompleted = false;
  91. this.buffer = [];
  92. this.active = 0;
  93. this.index = 0;
  94. }
  95. MergeMapToSubscriber.prototype._next = function (value) {
  96. if (this.active < this.concurrent) {
  97. var resultSelector = this.resultSelector;
  98. var index = this.index++;
  99. var ish = this.ish;
  100. var destination = this.destination;
  101. this.active++;
  102. this._innerSub(ish, destination, resultSelector, value, index);
  103. }
  104. else {
  105. this.buffer.push(value);
  106. }
  107. };
  108. MergeMapToSubscriber.prototype._innerSub = function (ish, destination, resultSelector, value, index) {
  109. this.add(subscribeToResult_1.subscribeToResult(this, ish, value, index));
  110. };
  111. MergeMapToSubscriber.prototype._complete = function () {
  112. this.hasCompleted = true;
  113. if (this.active === 0 && this.buffer.length === 0) {
  114. this.destination.complete();
  115. }
  116. };
  117. MergeMapToSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  118. var _a = this, resultSelector = _a.resultSelector, destination = _a.destination;
  119. if (resultSelector) {
  120. this.trySelectResult(outerValue, innerValue, outerIndex, innerIndex);
  121. }
  122. else {
  123. destination.next(innerValue);
  124. }
  125. };
  126. MergeMapToSubscriber.prototype.trySelectResult = function (outerValue, innerValue, outerIndex, innerIndex) {
  127. var _a = this, resultSelector = _a.resultSelector, destination = _a.destination;
  128. var result;
  129. try {
  130. result = resultSelector(outerValue, innerValue, outerIndex, innerIndex);
  131. }
  132. catch (err) {
  133. destination.error(err);
  134. return;
  135. }
  136. destination.next(result);
  137. };
  138. MergeMapToSubscriber.prototype.notifyError = function (err) {
  139. this.destination.error(err);
  140. };
  141. MergeMapToSubscriber.prototype.notifyComplete = function (innerSub) {
  142. var buffer = this.buffer;
  143. this.remove(innerSub);
  144. this.active--;
  145. if (buffer.length > 0) {
  146. this._next(buffer.shift());
  147. }
  148. else if (this.active === 0 && this.hasCompleted) {
  149. this.destination.complete();
  150. }
  151. };
  152. return MergeMapToSubscriber;
  153. }(OuterSubscriber_1.OuterSubscriber));
  154. exports.MergeMapToSubscriber = MergeMapToSubscriber;
  155. //# sourceMappingURL=mergeMapTo.js.map