Front end of the Slack clone application.

bufferCount.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /** PURE_IMPORTS_START .._Subscriber 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 { Subscriber } from '../Subscriber';
  10. /**
  11. * Buffers the source Observable values until the size hits the maximum
  12. * `bufferSize` given.
  13. *
  14. * <span class="informal">Collects values from the past as an array, and emits
  15. * that array only when its size reaches `bufferSize`.</span>
  16. *
  17. * <img src="./img/bufferCount.png" width="100%">
  18. *
  19. * Buffers a number of values from the source Observable by `bufferSize` then
  20. * emits the buffer and clears it, and starts a new buffer each
  21. * `startBufferEvery` values. If `startBufferEvery` is not provided or is
  22. * `null`, then new buffers are started immediately at the start of the source
  23. * and when each buffer closes and is emitted.
  24. *
  25. * @example <caption>Emit the last two click events as an array</caption>
  26. * var clicks = Rx.Observable.fromEvent(document, 'click');
  27. * var buffered = clicks.bufferCount(2);
  28. * buffered.subscribe(x => console.log(x));
  29. *
  30. * @example <caption>On every click, emit the last two click events as an array</caption>
  31. * var clicks = Rx.Observable.fromEvent(document, 'click');
  32. * var buffered = clicks.bufferCount(2, 1);
  33. * buffered.subscribe(x => console.log(x));
  34. *
  35. * @see {@link buffer}
  36. * @see {@link bufferTime}
  37. * @see {@link bufferToggle}
  38. * @see {@link bufferWhen}
  39. * @see {@link pairwise}
  40. * @see {@link windowCount}
  41. *
  42. * @param {number} bufferSize The maximum size of the buffer emitted.
  43. * @param {number} [startBufferEvery] Interval at which to start a new buffer.
  44. * For example if `startBufferEvery` is `2`, then a new buffer will be started
  45. * on every other value from the source. A new buffer is started at the
  46. * beginning of the source by default.
  47. * @return {Observable<T[]>} An Observable of arrays of buffered values.
  48. * @method bufferCount
  49. * @owner Observable
  50. */
  51. export function bufferCount(bufferSize, startBufferEvery) {
  52. if (startBufferEvery === void 0) {
  53. startBufferEvery = null;
  54. }
  55. return function bufferCountOperatorFunction(source) {
  56. return source.lift(new BufferCountOperator(bufferSize, startBufferEvery));
  57. };
  58. }
  59. var BufferCountOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  60. function BufferCountOperator(bufferSize, startBufferEvery) {
  61. this.bufferSize = bufferSize;
  62. this.startBufferEvery = startBufferEvery;
  63. if (!startBufferEvery || bufferSize === startBufferEvery) {
  64. this.subscriberClass = BufferCountSubscriber;
  65. }
  66. else {
  67. this.subscriberClass = BufferSkipCountSubscriber;
  68. }
  69. }
  70. BufferCountOperator.prototype.call = function (subscriber, source) {
  71. return source.subscribe(new this.subscriberClass(subscriber, this.bufferSize, this.startBufferEvery));
  72. };
  73. return BufferCountOperator;
  74. }());
  75. /**
  76. * We need this JSDoc comment for affecting ESDoc.
  77. * @ignore
  78. * @extends {Ignored}
  79. */
  80. var BufferCountSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  81. __extends(BufferCountSubscriber, _super);
  82. function BufferCountSubscriber(destination, bufferSize) {
  83. _super.call(this, destination);
  84. this.bufferSize = bufferSize;
  85. this.buffer = [];
  86. }
  87. BufferCountSubscriber.prototype._next = function (value) {
  88. var buffer = this.buffer;
  89. buffer.push(value);
  90. if (buffer.length == this.bufferSize) {
  91. this.destination.next(buffer);
  92. this.buffer = [];
  93. }
  94. };
  95. BufferCountSubscriber.prototype._complete = function () {
  96. var buffer = this.buffer;
  97. if (buffer.length > 0) {
  98. this.destination.next(buffer);
  99. }
  100. _super.prototype._complete.call(this);
  101. };
  102. return BufferCountSubscriber;
  103. }(Subscriber));
  104. /**
  105. * We need this JSDoc comment for affecting ESDoc.
  106. * @ignore
  107. * @extends {Ignored}
  108. */
  109. var BufferSkipCountSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  110. __extends(BufferSkipCountSubscriber, _super);
  111. function BufferSkipCountSubscriber(destination, bufferSize, startBufferEvery) {
  112. _super.call(this, destination);
  113. this.bufferSize = bufferSize;
  114. this.startBufferEvery = startBufferEvery;
  115. this.buffers = [];
  116. this.count = 0;
  117. }
  118. BufferSkipCountSubscriber.prototype._next = function (value) {
  119. var _a = this, bufferSize = _a.bufferSize, startBufferEvery = _a.startBufferEvery, buffers = _a.buffers, count = _a.count;
  120. this.count++;
  121. if (count % startBufferEvery === 0) {
  122. buffers.push([]);
  123. }
  124. for (var i = buffers.length; i--;) {
  125. var buffer = buffers[i];
  126. buffer.push(value);
  127. if (buffer.length === bufferSize) {
  128. buffers.splice(i, 1);
  129. this.destination.next(buffer);
  130. }
  131. }
  132. };
  133. BufferSkipCountSubscriber.prototype._complete = function () {
  134. var _a = this, buffers = _a.buffers, destination = _a.destination;
  135. while (buffers.length > 0) {
  136. var buffer = buffers.shift();
  137. if (buffer.length > 0) {
  138. destination.next(buffer);
  139. }
  140. }
  141. _super.prototype._complete.call(this);
  142. };
  143. return BufferSkipCountSubscriber;
  144. }(Subscriber));
  145. //# sourceMappingURL=bufferCount.js.map