a zip code crypto-currency system good for red ONLY

exhaustMap.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 an Observable which is merged in the output
  12. * Observable only if the previous projected Observable has completed.
  13. *
  14. * <span class="informal">Maps each value to an Observable, then flattens all of
  15. * these inner Observables using {@link exhaust}.</span>
  16. *
  17. * <img src="./img/exhaustMap.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 (so-called "inner") Observable. When it projects a source value to
  22. * an Observable, the output Observable begins emitting the items emitted by
  23. * that projected Observable. However, `exhaustMap` ignores every new projected
  24. * Observable if the previous projected Observable has not yet completed. Once
  25. * that one completes, it will accept and flatten the next projected Observable
  26. * and repeat this process.
  27. *
  28. * @example <caption>Run a finite timer for each click, only if there is no currently active timer</caption>
  29. * var clicks = Rx.Observable.fromEvent(document, 'click');
  30. * var result = clicks.exhaustMap((ev) => Rx.Observable.interval(1000).take(5));
  31. * result.subscribe(x => console.log(x));
  32. *
  33. * @see {@link concatMap}
  34. * @see {@link exhaust}
  35. * @see {@link mergeMap}
  36. * @see {@link switchMap}
  37. *
  38. * @param {function(value: T, ?index: number): ObservableInput} project A function
  39. * that, when applied to an item emitted by the source Observable, returns an
  40. * Observable.
  41. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  42. * A function to produce the value on the output Observable based on the values
  43. * and the indices of the source (outer) emission and the inner Observable
  44. * emission. The arguments passed to this function are:
  45. * - `outerValue`: the value that came from the source
  46. * - `innerValue`: the value that came from the projected Observable
  47. * - `outerIndex`: the "index" of the value that came from the source
  48. * - `innerIndex`: the "index" of the value from the projected Observable
  49. * @return {Observable} An Observable containing projected Observables
  50. * of each item of the source, ignoring projected Observables that start before
  51. * their preceding Observable has completed.
  52. * @method exhaustMap
  53. * @owner Observable
  54. */
  55. function exhaustMap(project, resultSelector) {
  56. return function (source) { return source.lift(new SwitchFirstMapOperator(project, resultSelector)); };
  57. }
  58. exports.exhaustMap = exhaustMap;
  59. var SwitchFirstMapOperator = (function () {
  60. function SwitchFirstMapOperator(project, resultSelector) {
  61. this.project = project;
  62. this.resultSelector = resultSelector;
  63. }
  64. SwitchFirstMapOperator.prototype.call = function (subscriber, source) {
  65. return source.subscribe(new SwitchFirstMapSubscriber(subscriber, this.project, this.resultSelector));
  66. };
  67. return SwitchFirstMapOperator;
  68. }());
  69. /**
  70. * We need this JSDoc comment for affecting ESDoc.
  71. * @ignore
  72. * @extends {Ignored}
  73. */
  74. var SwitchFirstMapSubscriber = (function (_super) {
  75. __extends(SwitchFirstMapSubscriber, _super);
  76. function SwitchFirstMapSubscriber(destination, project, resultSelector) {
  77. _super.call(this, destination);
  78. this.project = project;
  79. this.resultSelector = resultSelector;
  80. this.hasSubscription = false;
  81. this.hasCompleted = false;
  82. this.index = 0;
  83. }
  84. SwitchFirstMapSubscriber.prototype._next = function (value) {
  85. if (!this.hasSubscription) {
  86. this.tryNext(value);
  87. }
  88. };
  89. SwitchFirstMapSubscriber.prototype.tryNext = function (value) {
  90. var index = this.index++;
  91. var destination = this.destination;
  92. try {
  93. var result = this.project(value, index);
  94. this.hasSubscription = true;
  95. this.add(subscribeToResult_1.subscribeToResult(this, result, value, index));
  96. }
  97. catch (err) {
  98. destination.error(err);
  99. }
  100. };
  101. SwitchFirstMapSubscriber.prototype._complete = function () {
  102. this.hasCompleted = true;
  103. if (!this.hasSubscription) {
  104. this.destination.complete();
  105. }
  106. };
  107. SwitchFirstMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  108. var _a = this, resultSelector = _a.resultSelector, destination = _a.destination;
  109. if (resultSelector) {
  110. this.trySelectResult(outerValue, innerValue, outerIndex, innerIndex);
  111. }
  112. else {
  113. destination.next(innerValue);
  114. }
  115. };
  116. SwitchFirstMapSubscriber.prototype.trySelectResult = function (outerValue, innerValue, outerIndex, innerIndex) {
  117. var _a = this, resultSelector = _a.resultSelector, destination = _a.destination;
  118. try {
  119. var result = resultSelector(outerValue, innerValue, outerIndex, innerIndex);
  120. destination.next(result);
  121. }
  122. catch (err) {
  123. destination.error(err);
  124. }
  125. };
  126. SwitchFirstMapSubscriber.prototype.notifyError = function (err) {
  127. this.destination.error(err);
  128. };
  129. SwitchFirstMapSubscriber.prototype.notifyComplete = function (innerSub) {
  130. this.remove(innerSub);
  131. this.hasSubscription = false;
  132. if (this.hasCompleted) {
  133. this.destination.complete();
  134. }
  135. };
  136. return SwitchFirstMapSubscriber;
  137. }(OuterSubscriber_1.OuterSubscriber));
  138. //# sourceMappingURL=exhaustMap.js.map