Front end of the Slack clone application.

mergeScan.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { tryCatch } from '../util/tryCatch';
  2. import { errorObject } from '../util/errorObject';
  3. import { subscribeToResult } from '../util/subscribeToResult';
  4. import { OuterSubscriber } from '../OuterSubscriber';
  5. /**
  6. * Applies an accumulator function over the source Observable where the
  7. * accumulator function itself returns an Observable, then each intermediate
  8. * Observable returned is merged into the output Observable.
  9. *
  10. * <span class="informal">It's like {@link scan}, but the Observables returned
  11. * by the accumulator are merged into the outer Observable.</span>
  12. *
  13. * @example <caption>Count the number of click events</caption>
  14. * const click$ = Rx.Observable.fromEvent(document, 'click');
  15. * const one$ = click$.mapTo(1);
  16. * const seed = 0;
  17. * const count$ = one$.mergeScan((acc, one) => Rx.Observable.of(acc + one), seed);
  18. * count$.subscribe(x => console.log(x));
  19. *
  20. * // Results:
  21. * 1
  22. * 2
  23. * 3
  24. * 4
  25. * // ...and so on for each click
  26. *
  27. * @param {function(acc: R, value: T): Observable<R>} accumulator
  28. * The accumulator function called on each source value.
  29. * @param seed The initial accumulation value.
  30. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of
  31. * input Observables being subscribed to concurrently.
  32. * @return {Observable<R>} An observable of the accumulated values.
  33. * @method mergeScan
  34. * @owner Observable
  35. */
  36. export function mergeScan(accumulator, seed, concurrent = Number.POSITIVE_INFINITY) {
  37. return (source) => source.lift(new MergeScanOperator(accumulator, seed, concurrent));
  38. }
  39. export class MergeScanOperator {
  40. constructor(accumulator, seed, concurrent) {
  41. this.accumulator = accumulator;
  42. this.seed = seed;
  43. this.concurrent = concurrent;
  44. }
  45. call(subscriber, source) {
  46. return source.subscribe(new MergeScanSubscriber(subscriber, this.accumulator, this.seed, this.concurrent));
  47. }
  48. }
  49. /**
  50. * We need this JSDoc comment for affecting ESDoc.
  51. * @ignore
  52. * @extends {Ignored}
  53. */
  54. export class MergeScanSubscriber extends OuterSubscriber {
  55. constructor(destination, accumulator, acc, concurrent) {
  56. super(destination);
  57. this.accumulator = accumulator;
  58. this.acc = acc;
  59. this.concurrent = concurrent;
  60. this.hasValue = false;
  61. this.hasCompleted = false;
  62. this.buffer = [];
  63. this.active = 0;
  64. this.index = 0;
  65. }
  66. _next(value) {
  67. if (this.active < this.concurrent) {
  68. const index = this.index++;
  69. const ish = tryCatch(this.accumulator)(this.acc, value);
  70. const destination = this.destination;
  71. if (ish === errorObject) {
  72. destination.error(errorObject.e);
  73. }
  74. else {
  75. this.active++;
  76. this._innerSub(ish, value, index);
  77. }
  78. }
  79. else {
  80. this.buffer.push(value);
  81. }
  82. }
  83. _innerSub(ish, value, index) {
  84. this.add(subscribeToResult(this, ish, value, index));
  85. }
  86. _complete() {
  87. this.hasCompleted = true;
  88. if (this.active === 0 && this.buffer.length === 0) {
  89. if (this.hasValue === false) {
  90. this.destination.next(this.acc);
  91. }
  92. this.destination.complete();
  93. }
  94. }
  95. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  96. const { destination } = this;
  97. this.acc = innerValue;
  98. this.hasValue = true;
  99. destination.next(innerValue);
  100. }
  101. notifyComplete(innerSub) {
  102. const buffer = this.buffer;
  103. this.remove(innerSub);
  104. this.active--;
  105. if (buffer.length > 0) {
  106. this._next(buffer.shift());
  107. }
  108. else if (this.active === 0 && this.hasCompleted) {
  109. if (this.hasValue === false) {
  110. this.destination.next(this.acc);
  111. }
  112. this.destination.complete();
  113. }
  114. }
  115. }
  116. //# sourceMappingURL=mergeScan.js.map