Front end of the Slack clone application.

mergeMapTo.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 merged multiple
  6. * times in the output Observable.
  7. *
  8. * <span class="informal">It's like {@link mergeMap}, but maps each value always
  9. * to the same inner Observable.</span>
  10. *
  11. * <img src="./img/mergeMapTo.png" width="100%">
  12. *
  13. * Maps each source value to the given Observable `innerObservable` regardless
  14. * of the source value, and then merges those resulting Observables into one
  15. * single Observable, which is the output Observable.
  16. *
  17. * @example <caption>For each click event, start an interval Observable ticking every 1 second</caption>
  18. * var clicks = Rx.Observable.fromEvent(document, 'click');
  19. * var result = clicks.mergeMapTo(Rx.Observable.interval(1000));
  20. * result.subscribe(x => console.log(x));
  21. *
  22. * @see {@link concatMapTo}
  23. * @see {@link merge}
  24. * @see {@link mergeAll}
  25. * @see {@link mergeMap}
  26. * @see {@link mergeScan}
  27. * @see {@link switchMapTo}
  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. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
  40. * Observables being subscribed to concurrently.
  41. * @return {Observable} An Observable that emits items from the given
  42. * `innerObservable` (and optionally transformed through `resultSelector`) every
  43. * time a value is emitted on the source Observable.
  44. * @method mergeMapTo
  45. * @owner Observable
  46. */
  47. export function mergeMapTo(innerObservable, resultSelector, concurrent = Number.POSITIVE_INFINITY) {
  48. if (typeof resultSelector === 'number') {
  49. concurrent = resultSelector;
  50. resultSelector = null;
  51. }
  52. return (source) => source.lift(new MergeMapToOperator(innerObservable, resultSelector, concurrent));
  53. }
  54. // TODO: Figure out correct signature here: an Operator<Observable<T>, R>
  55. // needs to implement call(observer: Subscriber<R>): Subscriber<Observable<T>>
  56. export class MergeMapToOperator {
  57. constructor(ish, resultSelector, concurrent = Number.POSITIVE_INFINITY) {
  58. this.ish = ish;
  59. this.resultSelector = resultSelector;
  60. this.concurrent = concurrent;
  61. }
  62. call(observer, source) {
  63. return source.subscribe(new MergeMapToSubscriber(observer, this.ish, this.resultSelector, this.concurrent));
  64. }
  65. }
  66. /**
  67. * We need this JSDoc comment for affecting ESDoc.
  68. * @ignore
  69. * @extends {Ignored}
  70. */
  71. export class MergeMapToSubscriber extends OuterSubscriber {
  72. constructor(destination, ish, resultSelector, concurrent = Number.POSITIVE_INFINITY) {
  73. super(destination);
  74. this.ish = ish;
  75. this.resultSelector = resultSelector;
  76. this.concurrent = concurrent;
  77. this.hasCompleted = false;
  78. this.buffer = [];
  79. this.active = 0;
  80. this.index = 0;
  81. }
  82. _next(value) {
  83. if (this.active < this.concurrent) {
  84. const resultSelector = this.resultSelector;
  85. const index = this.index++;
  86. const ish = this.ish;
  87. const destination = this.destination;
  88. this.active++;
  89. this._innerSub(ish, destination, resultSelector, value, index);
  90. }
  91. else {
  92. this.buffer.push(value);
  93. }
  94. }
  95. _innerSub(ish, destination, resultSelector, value, index) {
  96. this.add(subscribeToResult(this, ish, value, index));
  97. }
  98. _complete() {
  99. this.hasCompleted = true;
  100. if (this.active === 0 && this.buffer.length === 0) {
  101. this.destination.complete();
  102. }
  103. }
  104. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  105. const { resultSelector, destination } = this;
  106. if (resultSelector) {
  107. this.trySelectResult(outerValue, innerValue, outerIndex, innerIndex);
  108. }
  109. else {
  110. destination.next(innerValue);
  111. }
  112. }
  113. trySelectResult(outerValue, innerValue, outerIndex, innerIndex) {
  114. const { resultSelector, destination } = this;
  115. let result;
  116. try {
  117. result = resultSelector(outerValue, innerValue, outerIndex, innerIndex);
  118. }
  119. catch (err) {
  120. destination.error(err);
  121. return;
  122. }
  123. destination.next(result);
  124. }
  125. notifyError(err) {
  126. this.destination.error(err);
  127. }
  128. notifyComplete(innerSub) {
  129. const buffer = this.buffer;
  130. this.remove(innerSub);
  131. this.active--;
  132. if (buffer.length > 0) {
  133. this._next(buffer.shift());
  134. }
  135. else if (this.active === 0 && this.hasCompleted) {
  136. this.destination.complete();
  137. }
  138. }
  139. }
  140. //# sourceMappingURL=mergeMapTo.js.map