Front end of the Slack clone application.

switchMap.js 5.9KB

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