switchMapTo.js 4.4 KB

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