bufferCount.js 5.3 KB

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