switchMap.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, emitting values only from the most recently projected Observable.
  13. *
  14. * <span class="informal">Maps each value to an Observable, then flattens all of
  15. * these inner Observables using {@link switch}.</span>
  16. *
  17. * <img src="./img/switchMap.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. Each time it observes one of these
  22. * inner Observables, the output Observable begins emitting the items emitted by
  23. * that inner Observable. When a new inner Observable is emitted, `switchMap`
  24. * stops emitting items from the earlier-emitted inner Observable and begins
  25. * emitting items from the new one. It continues to behave like this for
  26. * subsequent inner Observables.
  27. *
  28. * @example <caption>Rerun an interval Observable on every click event</caption>
  29. * var clicks = Rx.Observable.fromEvent(document, 'click');
  30. * var result = clicks.switchMap((ev) => Rx.Observable.interval(1000));
  31. * result.subscribe(x => console.log(x));
  32. *
  33. * @see {@link concatMap}
  34. * @see {@link exhaustMap}
  35. * @see {@link mergeMap}
  36. * @see {@link switch}
  37. * @see {@link switchMapTo}
  38. *
  39. * @param {function(value: T, ?index: number): ObservableInput} project A function
  40. * that, when applied to an item emitted by the source Observable, returns an
  41. * Observable.
  42. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  43. * A function to produce the value on the output Observable based on the values
  44. * and the indices of the source (outer) emission and the inner Observable
  45. * emission. The arguments passed to this function are:
  46. * - `outerValue`: the value that came from the source
  47. * - `innerValue`: the value that came from the projected Observable
  48. * - `outerIndex`: the "index" of the value that came from the source
  49. * - `innerIndex`: the "index" of the value from the projected Observable
  50. * @return {Observable} An Observable that emits the result of applying the
  51. * projection function (and the optional `resultSelector`) to each item emitted
  52. * by the source Observable and taking only the values from the most recently
  53. * projected inner Observable.
  54. * @method switchMap
  55. * @owner Observable
  56. */
  57. function switchMap(project, resultSelector) {
  58. return function switchMapOperatorFunction(source) {
  59. return source.lift(new SwitchMapOperator(project, resultSelector));
  60. };
  61. }
  62. exports.switchMap = switchMap;
  63. var SwitchMapOperator = (function () {
  64. function SwitchMapOperator(project, resultSelector) {
  65. this.project = project;
  66. this.resultSelector = resultSelector;
  67. }
  68. SwitchMapOperator.prototype.call = function (subscriber, source) {
  69. return source.subscribe(new SwitchMapSubscriber(subscriber, this.project, this.resultSelector));
  70. };
  71. return SwitchMapOperator;
  72. }());
  73. /**
  74. * We need this JSDoc comment for affecting ESDoc.
  75. * @ignore
  76. * @extends {Ignored}
  77. */
  78. var SwitchMapSubscriber = (function (_super) {
  79. __extends(SwitchMapSubscriber, _super);
  80. function SwitchMapSubscriber(destination, project, resultSelector) {
  81. _super.call(this, destination);
  82. this.project = project;
  83. this.resultSelector = resultSelector;
  84. this.index = 0;
  85. }
  86. SwitchMapSubscriber.prototype._next = function (value) {
  87. var result;
  88. var index = this.index++;
  89. try {
  90. result = this.project(value, index);
  91. }
  92. catch (error) {
  93. this.destination.error(error);
  94. return;
  95. }
  96. this._innerSub(result, value, index);
  97. };
  98. SwitchMapSubscriber.prototype._innerSub = function (result, value, index) {
  99. var innerSubscription = this.innerSubscription;
  100. if (innerSubscription) {
  101. innerSubscription.unsubscribe();
  102. }
  103. this.add(this.innerSubscription = subscribeToResult_1.subscribeToResult(this, result, value, index));
  104. };
  105. SwitchMapSubscriber.prototype._complete = function () {
  106. var innerSubscription = this.innerSubscription;
  107. if (!innerSubscription || innerSubscription.closed) {
  108. _super.prototype._complete.call(this);
  109. }
  110. };
  111. /** @deprecated internal use only */ SwitchMapSubscriber.prototype._unsubscribe = function () {
  112. this.innerSubscription = null;
  113. };
  114. SwitchMapSubscriber.prototype.notifyComplete = function (innerSub) {
  115. this.remove(innerSub);
  116. this.innerSubscription = null;
  117. if (this.isStopped) {
  118. _super.prototype._complete.call(this);
  119. }
  120. };
  121. SwitchMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  122. if (this.resultSelector) {
  123. this._tryNotifyNext(outerValue, innerValue, outerIndex, innerIndex);
  124. }
  125. else {
  126. this.destination.next(innerValue);
  127. }
  128. };
  129. SwitchMapSubscriber.prototype._tryNotifyNext = function (outerValue, innerValue, outerIndex, innerIndex) {
  130. var result;
  131. try {
  132. result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex);
  133. }
  134. catch (err) {
  135. this.destination.error(err);
  136. return;
  137. }
  138. this.destination.next(result);
  139. };
  140. return SwitchMapSubscriber;
  141. }(OuterSubscriber_1.OuterSubscriber));
  142. //# sourceMappingURL=switchMap.js.map