switchMapTo.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 the same Observable which is flattened multiple
  14. * times with {@link switch} in the output Observable.
  15. *
  16. * <span class="informal">It's like {@link switchMap}, but maps each value
  17. * always to the same inner Observable.</span>
  18. *
  19. * <img src="./img/switchMapTo.png" width="100%">
  20. *
  21. * Maps each source value to the given Observable `innerObservable` regardless
  22. * of the source value, and then flattens those resulting Observables into one
  23. * single Observable, which is the output Observable. The output Observables
  24. * emits values only from the most recently emitted instance of
  25. * `innerObservable`.
  26. *
  27. * @example <caption>Rerun an interval Observable on every click event</caption>
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var result = clicks.switchMapTo(Rx.Observable.interval(1000));
  30. * result.subscribe(x => console.log(x));
  31. *
  32. * @see {@link concatMapTo}
  33. * @see {@link switch}
  34. * @see {@link switchMap}
  35. * @see {@link mergeMapTo}
  36. *
  37. * @param {ObservableInput} innerObservable An Observable to replace each value from
  38. * the source Observable.
  39. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector]
  40. * A function to produce the value on the output Observable based on the values
  41. * and the indices of the source (outer) emission and the inner Observable
  42. * emission. The arguments passed to this function are:
  43. * - `outerValue`: the value that came from the source
  44. * - `innerValue`: the value that came from the projected Observable
  45. * - `outerIndex`: the "index" of the value that came from the source
  46. * - `innerIndex`: the "index" of the value from the projected Observable
  47. * @return {Observable} An Observable that emits items from the given
  48. * `innerObservable` (and optionally transformed through `resultSelector`) every
  49. * time a value is emitted on the source Observable, and taking only the values
  50. * from the most recently projected inner Observable.
  51. * @method switchMapTo
  52. * @owner Observable
  53. */
  54. export function switchMapTo(innerObservable, resultSelector) {
  55. return function (source) { return source.lift(new SwitchMapToOperator(innerObservable, resultSelector)); };
  56. }
  57. var SwitchMapToOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  58. function SwitchMapToOperator(observable, resultSelector) {
  59. this.observable = observable;
  60. this.resultSelector = resultSelector;
  61. }
  62. SwitchMapToOperator.prototype.call = function (subscriber, source) {
  63. return source.subscribe(new SwitchMapToSubscriber(subscriber, this.observable, this.resultSelector));
  64. };
  65. return SwitchMapToOperator;
  66. }());
  67. /**
  68. * We need this JSDoc comment for affecting ESDoc.
  69. * @ignore
  70. * @extends {Ignored}
  71. */
  72. var SwitchMapToSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  73. __extends(SwitchMapToSubscriber, _super);
  74. function SwitchMapToSubscriber(destination, inner, resultSelector) {
  75. _super.call(this, destination);
  76. this.inner = inner;
  77. this.resultSelector = resultSelector;
  78. this.index = 0;
  79. }
  80. SwitchMapToSubscriber.prototype._next = function (value) {
  81. var innerSubscription = this.innerSubscription;
  82. if (innerSubscription) {
  83. innerSubscription.unsubscribe();
  84. }
  85. this.add(this.innerSubscription = subscribeToResult(this, this.inner, value, this.index++));
  86. };
  87. SwitchMapToSubscriber.prototype._complete = function () {
  88. var innerSubscription = this.innerSubscription;
  89. if (!innerSubscription || innerSubscription.closed) {
  90. _super.prototype._complete.call(this);
  91. }
  92. };
  93. /** @deprecated internal use only */ SwitchMapToSubscriber.prototype._unsubscribe = function () {
  94. this.innerSubscription = null;
  95. };
  96. SwitchMapToSubscriber.prototype.notifyComplete = function (innerSub) {
  97. this.remove(innerSub);
  98. this.innerSubscription = null;
  99. if (this.isStopped) {
  100. _super.prototype._complete.call(this);
  101. }
  102. };
  103. SwitchMapToSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  104. var _a = this, resultSelector = _a.resultSelector, destination = _a.destination;
  105. if (resultSelector) {
  106. this.tryResultSelector(outerValue, innerValue, outerIndex, innerIndex);
  107. }
  108. else {
  109. destination.next(innerValue);
  110. }
  111. };
  112. SwitchMapToSubscriber.prototype.tryResultSelector = function (outerValue, innerValue, outerIndex, innerIndex) {
  113. var _a = this, resultSelector = _a.resultSelector, destination = _a.destination;
  114. var result;
  115. try {
  116. result = resultSelector(outerValue, innerValue, outerIndex, innerIndex);
  117. }
  118. catch (err) {
  119. destination.error(err);
  120. return;
  121. }
  122. destination.next(result);
  123. };
  124. return SwitchMapToSubscriber;
  125. }(OuterSubscriber));
  126. //# sourceMappingURL=switchMapTo.js.map