a zip code crypto-currency system good for red ONLY

mergeMap.js 5.5KB

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