switchMapTo.js 5.3 KB

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