a zip code crypto-currency system good for red ONLY

mergeMap.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 subscribeToResult_1 = require('../util/subscribeToResult');
  8. var OuterSubscriber_1 = require('../OuterSubscriber');
  9. /* tslint:enable:max-line-length */
  10. /**
  11. * Projects each source value to an Observable which is merged in the output
  12. * Observable.
  13. *
  14. * <span class="informal">Maps each value to an Observable, then flattens all of
  15. * these inner Observables using {@link mergeAll}.</span>
  16. *
  17. * <img src="./img/mergeMap.png" width="100%">
  18. *
  19. * Returns an Observable that emits items based on applying a function that you
  20. * supply to each item emitted by the source Observable, where that function
  21. * returns an Observable, and then merging those resulting Observables and
  22. * emitting the results of this merger.
  23. *
  24. * @example <caption>Map and flatten each letter to an Observable ticking every 1 second</caption>
  25. * var letters = Rx.Observable.of('a', 'b', 'c');
  26. * var result = letters.mergeMap(x =>
  27. * Rx.Observable.interval(1000).map(i => x+i)
  28. * );
  29. * result.subscribe(x => console.log(x));
  30. *
  31. * // Results in the following:
  32. * // a0
  33. * // b0
  34. * // c0
  35. * // a1
  36. * // b1
  37. * // c1
  38. * // continues to list a,b,c with respective ascending integers
  39. *
  40. * @see {@link concatMap}
  41. * @see {@link exhaustMap}
  42. * @see {@link merge}
  43. * @see {@link mergeAll}
  44. * @see {@link mergeMapTo}
  45. * @see {@link mergeScan}
  46. * @see {@link switchMap}
  47. *
  48. * @param {function(value: T, ?index: number): ObservableInput} project A function
  49. * that, when applied to an item emitted by the source Observable, returns an
  50. * Observable.
  51. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  52. * A function to produce the value on the output Observable based on the values
  53. * and the indices of the source (outer) emission and the inner Observable
  54. * emission. The arguments passed to this function are:
  55. * - `outerValue`: the value that came from the source
  56. * - `innerValue`: the value that came from the projected Observable
  57. * - `outerIndex`: the "index" of the value that came from the source
  58. * - `innerIndex`: the "index" of the value from the projected Observable
  59. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
  60. * Observables being subscribed to concurrently.
  61. * @return {Observable} An Observable that emits the result of applying the
  62. * projection function (and the optional `resultSelector`) to each item emitted
  63. * by the source Observable and merging the results of the Observables obtained
  64. * from this transformation.
  65. * @method mergeMap
  66. * @owner Observable
  67. */
  68. function mergeMap(project, resultSelector, concurrent) {
  69. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  70. return function mergeMapOperatorFunction(source) {
  71. if (typeof resultSelector === 'number') {
  72. concurrent = resultSelector;
  73. resultSelector = null;
  74. }
  75. return source.lift(new MergeMapOperator(project, resultSelector, concurrent));
  76. };
  77. }
  78. exports.mergeMap = mergeMap;
  79. var MergeMapOperator = (function () {
  80. function MergeMapOperator(project, resultSelector, concurrent) {
  81. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  82. this.project = project;
  83. this.resultSelector = resultSelector;
  84. this.concurrent = concurrent;
  85. }
  86. MergeMapOperator.prototype.call = function (observer, source) {
  87. return source.subscribe(new MergeMapSubscriber(observer, this.project, this.resultSelector, this.concurrent));
  88. };
  89. return MergeMapOperator;
  90. }());
  91. exports.MergeMapOperator = MergeMapOperator;
  92. /**
  93. * We need this JSDoc comment for affecting ESDoc.
  94. * @ignore
  95. * @extends {Ignored}
  96. */
  97. var MergeMapSubscriber = (function (_super) {
  98. __extends(MergeMapSubscriber, _super);
  99. function MergeMapSubscriber(destination, project, resultSelector, concurrent) {
  100. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  101. _super.call(this, destination);
  102. this.project = project;
  103. this.resultSelector = resultSelector;
  104. this.concurrent = concurrent;
  105. this.hasCompleted = false;
  106. this.buffer = [];
  107. this.active = 0;
  108. this.index = 0;
  109. }
  110. MergeMapSubscriber.prototype._next = function (value) {
  111. if (this.active < this.concurrent) {
  112. this._tryNext(value);
  113. }
  114. else {
  115. this.buffer.push(value);
  116. }
  117. };
  118. MergeMapSubscriber.prototype._tryNext = function (value) {
  119. var result;
  120. var index = this.index++;
  121. try {
  122. result = this.project(value, index);
  123. }
  124. catch (err) {
  125. this.destination.error(err);
  126. return;
  127. }
  128. this.active++;
  129. this._innerSub(result, value, index);
  130. };
  131. MergeMapSubscriber.prototype._innerSub = function (ish, value, index) {
  132. this.add(subscribeToResult_1.subscribeToResult(this, ish, value, index));
  133. };
  134. MergeMapSubscriber.prototype._complete = function () {
  135. this.hasCompleted = true;
  136. if (this.active === 0 && this.buffer.length === 0) {
  137. this.destination.complete();
  138. }
  139. };
  140. MergeMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  141. if (this.resultSelector) {
  142. this._notifyResultSelector(outerValue, innerValue, outerIndex, innerIndex);
  143. }
  144. else {
  145. this.destination.next(innerValue);
  146. }
  147. };
  148. MergeMapSubscriber.prototype._notifyResultSelector = function (outerValue, innerValue, outerIndex, innerIndex) {
  149. var result;
  150. try {
  151. result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex);
  152. }
  153. catch (err) {
  154. this.destination.error(err);
  155. return;
  156. }
  157. this.destination.next(result);
  158. };
  159. MergeMapSubscriber.prototype.notifyComplete = function (innerSub) {
  160. var buffer = this.buffer;
  161. this.remove(innerSub);
  162. this.active--;
  163. if (buffer.length > 0) {
  164. this._next(buffer.shift());
  165. }
  166. else if (this.active === 0 && this.hasCompleted) {
  167. this.destination.complete();
  168. }
  169. };
  170. return MergeMapSubscriber;
  171. }(OuterSubscriber_1.OuterSubscriber));
  172. exports.MergeMapSubscriber = MergeMapSubscriber;
  173. //# sourceMappingURL=mergeMap.js.map