bufferCount.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { operate } from '../util/lift';
  2. import { createOperatorSubscriber } from './OperatorSubscriber';
  3. import { arrRemove } from '../util/arrRemove';
  4. export function bufferCount(bufferSize, startBufferEvery = null) {
  5. startBufferEvery = startBufferEvery !== null && startBufferEvery !== void 0 ? startBufferEvery : bufferSize;
  6. return operate((source, subscriber) => {
  7. let buffers = [];
  8. let count = 0;
  9. source.subscribe(createOperatorSubscriber(subscriber, (value) => {
  10. let toEmit = null;
  11. if (count++ % startBufferEvery === 0) {
  12. buffers.push([]);
  13. }
  14. for (const buffer of buffers) {
  15. buffer.push(value);
  16. if (bufferSize <= buffer.length) {
  17. toEmit = toEmit !== null && toEmit !== void 0 ? toEmit : [];
  18. toEmit.push(buffer);
  19. }
  20. }
  21. if (toEmit) {
  22. for (const buffer of toEmit) {
  23. arrRemove(buffers, buffer);
  24. subscriber.next(buffer);
  25. }
  26. }
  27. }, () => {
  28. for (const buffer of buffers) {
  29. subscriber.next(buffer);
  30. }
  31. subscriber.complete();
  32. }, undefined, () => {
  33. buffers = null;
  34. }));
  35. });
  36. }
  37. //# sourceMappingURL=bufferCount.js.map