mergeMapTo.js 6.5KB

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