bufferCount.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** PURE_IMPORTS_START tslib,_Subscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. export function bufferCount(bufferSize, startBufferEvery) {
  5. if (startBufferEvery === void 0) {
  6. startBufferEvery = null;
  7. }
  8. return function bufferCountOperatorFunction(source) {
  9. return source.lift(new BufferCountOperator(bufferSize, startBufferEvery));
  10. };
  11. }
  12. var BufferCountOperator = /*@__PURE__*/ (function () {
  13. function BufferCountOperator(bufferSize, startBufferEvery) {
  14. this.bufferSize = bufferSize;
  15. this.startBufferEvery = startBufferEvery;
  16. if (!startBufferEvery || bufferSize === startBufferEvery) {
  17. this.subscriberClass = BufferCountSubscriber;
  18. }
  19. else {
  20. this.subscriberClass = BufferSkipCountSubscriber;
  21. }
  22. }
  23. BufferCountOperator.prototype.call = function (subscriber, source) {
  24. return source.subscribe(new this.subscriberClass(subscriber, this.bufferSize, this.startBufferEvery));
  25. };
  26. return BufferCountOperator;
  27. }());
  28. var BufferCountSubscriber = /*@__PURE__*/ (function (_super) {
  29. tslib_1.__extends(BufferCountSubscriber, _super);
  30. function BufferCountSubscriber(destination, bufferSize) {
  31. var _this = _super.call(this, destination) || this;
  32. _this.bufferSize = bufferSize;
  33. _this.buffer = [];
  34. return _this;
  35. }
  36. BufferCountSubscriber.prototype._next = function (value) {
  37. var buffer = this.buffer;
  38. buffer.push(value);
  39. if (buffer.length == this.bufferSize) {
  40. this.destination.next(buffer);
  41. this.buffer = [];
  42. }
  43. };
  44. BufferCountSubscriber.prototype._complete = function () {
  45. var buffer = this.buffer;
  46. if (buffer.length > 0) {
  47. this.destination.next(buffer);
  48. }
  49. _super.prototype._complete.call(this);
  50. };
  51. return BufferCountSubscriber;
  52. }(Subscriber));
  53. var BufferSkipCountSubscriber = /*@__PURE__*/ (function (_super) {
  54. tslib_1.__extends(BufferSkipCountSubscriber, _super);
  55. function BufferSkipCountSubscriber(destination, bufferSize, startBufferEvery) {
  56. var _this = _super.call(this, destination) || this;
  57. _this.bufferSize = bufferSize;
  58. _this.startBufferEvery = startBufferEvery;
  59. _this.buffers = [];
  60. _this.count = 0;
  61. return _this;
  62. }
  63. BufferSkipCountSubscriber.prototype._next = function (value) {
  64. var _a = this, bufferSize = _a.bufferSize, startBufferEvery = _a.startBufferEvery, buffers = _a.buffers, count = _a.count;
  65. this.count++;
  66. if (count % startBufferEvery === 0) {
  67. buffers.push([]);
  68. }
  69. for (var i = buffers.length; i--;) {
  70. var buffer = buffers[i];
  71. buffer.push(value);
  72. if (buffer.length === bufferSize) {
  73. buffers.splice(i, 1);
  74. this.destination.next(buffer);
  75. }
  76. }
  77. };
  78. BufferSkipCountSubscriber.prototype._complete = function () {
  79. var _a = this, buffers = _a.buffers, destination = _a.destination;
  80. while (buffers.length > 0) {
  81. var buffer = buffers.shift();
  82. if (buffer.length > 0) {
  83. destination.next(buffer);
  84. }
  85. }
  86. _super.prototype._complete.call(this);
  87. };
  88. return BufferSkipCountSubscriber;
  89. }(Subscriber));
  90. //# sourceMappingURL=bufferCount.js.map