bufferCount.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.bufferCount = void 0;
  15. var lift_1 = require("../util/lift");
  16. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  17. var arrRemove_1 = require("../util/arrRemove");
  18. function bufferCount(bufferSize, startBufferEvery) {
  19. if (startBufferEvery === void 0) { startBufferEvery = null; }
  20. startBufferEvery = startBufferEvery !== null && startBufferEvery !== void 0 ? startBufferEvery : bufferSize;
  21. return lift_1.operate(function (source, subscriber) {
  22. var buffers = [];
  23. var count = 0;
  24. source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  25. var e_1, _a, e_2, _b;
  26. var toEmit = null;
  27. if (count++ % startBufferEvery === 0) {
  28. buffers.push([]);
  29. }
  30. try {
  31. for (var buffers_1 = __values(buffers), buffers_1_1 = buffers_1.next(); !buffers_1_1.done; buffers_1_1 = buffers_1.next()) {
  32. var buffer = buffers_1_1.value;
  33. buffer.push(value);
  34. if (bufferSize <= buffer.length) {
  35. toEmit = toEmit !== null && toEmit !== void 0 ? toEmit : [];
  36. toEmit.push(buffer);
  37. }
  38. }
  39. }
  40. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  41. finally {
  42. try {
  43. if (buffers_1_1 && !buffers_1_1.done && (_a = buffers_1.return)) _a.call(buffers_1);
  44. }
  45. finally { if (e_1) throw e_1.error; }
  46. }
  47. if (toEmit) {
  48. try {
  49. for (var toEmit_1 = __values(toEmit), toEmit_1_1 = toEmit_1.next(); !toEmit_1_1.done; toEmit_1_1 = toEmit_1.next()) {
  50. var buffer = toEmit_1_1.value;
  51. arrRemove_1.arrRemove(buffers, buffer);
  52. subscriber.next(buffer);
  53. }
  54. }
  55. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  56. finally {
  57. try {
  58. if (toEmit_1_1 && !toEmit_1_1.done && (_b = toEmit_1.return)) _b.call(toEmit_1);
  59. }
  60. finally { if (e_2) throw e_2.error; }
  61. }
  62. }
  63. }, function () {
  64. var e_3, _a;
  65. try {
  66. for (var buffers_2 = __values(buffers), buffers_2_1 = buffers_2.next(); !buffers_2_1.done; buffers_2_1 = buffers_2.next()) {
  67. var buffer = buffers_2_1.value;
  68. subscriber.next(buffer);
  69. }
  70. }
  71. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  72. finally {
  73. try {
  74. if (buffers_2_1 && !buffers_2_1.done && (_a = buffers_2.return)) _a.call(buffers_2);
  75. }
  76. finally { if (e_3) throw e_3.error; }
  77. }
  78. subscriber.complete();
  79. }, undefined, function () {
  80. buffers = null;
  81. }));
  82. });
  83. }
  84. exports.bufferCount = bufferCount;
  85. //# sourceMappingURL=bufferCount.js.map