bufferCount.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { __values } from "tslib";
  2. import { operate } from '../util/lift';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. import { arrRemove } from '../util/arrRemove';
  5. export function bufferCount(bufferSize, startBufferEvery) {
  6. if (startBufferEvery === void 0) { startBufferEvery = null; }
  7. startBufferEvery = startBufferEvery !== null && startBufferEvery !== void 0 ? startBufferEvery : bufferSize;
  8. return operate(function (source, subscriber) {
  9. var buffers = [];
  10. var count = 0;
  11. source.subscribe(createOperatorSubscriber(subscriber, function (value) {
  12. var e_1, _a, e_2, _b;
  13. var toEmit = null;
  14. if (count++ % startBufferEvery === 0) {
  15. buffers.push([]);
  16. }
  17. try {
  18. for (var buffers_1 = __values(buffers), buffers_1_1 = buffers_1.next(); !buffers_1_1.done; buffers_1_1 = buffers_1.next()) {
  19. var buffer = buffers_1_1.value;
  20. buffer.push(value);
  21. if (bufferSize <= buffer.length) {
  22. toEmit = toEmit !== null && toEmit !== void 0 ? toEmit : [];
  23. toEmit.push(buffer);
  24. }
  25. }
  26. }
  27. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  28. finally {
  29. try {
  30. if (buffers_1_1 && !buffers_1_1.done && (_a = buffers_1.return)) _a.call(buffers_1);
  31. }
  32. finally { if (e_1) throw e_1.error; }
  33. }
  34. if (toEmit) {
  35. try {
  36. for (var toEmit_1 = __values(toEmit), toEmit_1_1 = toEmit_1.next(); !toEmit_1_1.done; toEmit_1_1 = toEmit_1.next()) {
  37. var buffer = toEmit_1_1.value;
  38. arrRemove(buffers, buffer);
  39. subscriber.next(buffer);
  40. }
  41. }
  42. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  43. finally {
  44. try {
  45. if (toEmit_1_1 && !toEmit_1_1.done && (_b = toEmit_1.return)) _b.call(toEmit_1);
  46. }
  47. finally { if (e_2) throw e_2.error; }
  48. }
  49. }
  50. }, function () {
  51. var e_3, _a;
  52. try {
  53. for (var buffers_2 = __values(buffers), buffers_2_1 = buffers_2.next(); !buffers_2_1.done; buffers_2_1 = buffers_2.next()) {
  54. var buffer = buffers_2_1.value;
  55. subscriber.next(buffer);
  56. }
  57. }
  58. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  59. finally {
  60. try {
  61. if (buffers_2_1 && !buffers_2_1.done && (_a = buffers_2.return)) _a.call(buffers_2);
  62. }
  63. finally { if (e_3) throw e_3.error; }
  64. }
  65. subscriber.complete();
  66. }, undefined, function () {
  67. buffers = null;
  68. }));
  69. });
  70. }
  71. //# sourceMappingURL=bufferCount.js.map