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