Front end of the Slack clone application.

bufferWhen.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Subscription } from '../Subscription';
  2. import { tryCatch } from '../util/tryCatch';
  3. import { errorObject } from '../util/errorObject';
  4. import { OuterSubscriber } from '../OuterSubscriber';
  5. import { subscribeToResult } from '../util/subscribeToResult';
  6. /**
  7. * Buffers the source Observable values, using a factory function of closing
  8. * Observables to determine when to close, emit, and reset the buffer.
  9. *
  10. * <span class="informal">Collects values from the past as an array. When it
  11. * starts collecting values, it calls a function that returns an Observable that
  12. * tells when to close the buffer and restart collecting.</span>
  13. *
  14. * <img src="./img/bufferWhen.png" width="100%">
  15. *
  16. * Opens a buffer immediately, then closes the buffer when the observable
  17. * returned by calling `closingSelector` function emits a value. When it closes
  18. * the buffer, it immediately opens a new buffer and repeats the process.
  19. *
  20. * @example <caption>Emit an array of the last clicks every [1-5] random seconds</caption>
  21. * var clicks = Rx.Observable.fromEvent(document, 'click');
  22. * var buffered = clicks.bufferWhen(() =>
  23. * Rx.Observable.interval(1000 + Math.random() * 4000)
  24. * );
  25. * buffered.subscribe(x => console.log(x));
  26. *
  27. * @see {@link buffer}
  28. * @see {@link bufferCount}
  29. * @see {@link bufferTime}
  30. * @see {@link bufferToggle}
  31. * @see {@link windowWhen}
  32. *
  33. * @param {function(): Observable} closingSelector A function that takes no
  34. * arguments and returns an Observable that signals buffer closure.
  35. * @return {Observable<T[]>} An observable of arrays of buffered values.
  36. * @method bufferWhen
  37. * @owner Observable
  38. */
  39. export function bufferWhen(closingSelector) {
  40. return function (source) {
  41. return source.lift(new BufferWhenOperator(closingSelector));
  42. };
  43. }
  44. class BufferWhenOperator {
  45. constructor(closingSelector) {
  46. this.closingSelector = closingSelector;
  47. }
  48. call(subscriber, source) {
  49. return source.subscribe(new BufferWhenSubscriber(subscriber, this.closingSelector));
  50. }
  51. }
  52. /**
  53. * We need this JSDoc comment for affecting ESDoc.
  54. * @ignore
  55. * @extends {Ignored}
  56. */
  57. class BufferWhenSubscriber extends OuterSubscriber {
  58. constructor(destination, closingSelector) {
  59. super(destination);
  60. this.closingSelector = closingSelector;
  61. this.subscribing = false;
  62. this.openBuffer();
  63. }
  64. _next(value) {
  65. this.buffer.push(value);
  66. }
  67. _complete() {
  68. const buffer = this.buffer;
  69. if (buffer) {
  70. this.destination.next(buffer);
  71. }
  72. super._complete();
  73. }
  74. /** @deprecated internal use only */ _unsubscribe() {
  75. this.buffer = null;
  76. this.subscribing = false;
  77. }
  78. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  79. this.openBuffer();
  80. }
  81. notifyComplete() {
  82. if (this.subscribing) {
  83. this.complete();
  84. }
  85. else {
  86. this.openBuffer();
  87. }
  88. }
  89. openBuffer() {
  90. let { closingSubscription } = this;
  91. if (closingSubscription) {
  92. this.remove(closingSubscription);
  93. closingSubscription.unsubscribe();
  94. }
  95. const buffer = this.buffer;
  96. if (this.buffer) {
  97. this.destination.next(buffer);
  98. }
  99. this.buffer = [];
  100. const closingNotifier = tryCatch(this.closingSelector)();
  101. if (closingNotifier === errorObject) {
  102. this.error(errorObject.e);
  103. }
  104. else {
  105. closingSubscription = new Subscription();
  106. this.closingSubscription = closingSubscription;
  107. this.add(closingSubscription);
  108. this.subscribing = true;
  109. closingSubscription.add(subscribeToResult(this, closingNotifier));
  110. this.subscribing = false;
  111. }
  112. }
  113. }
  114. //# sourceMappingURL=bufferWhen.js.map