Batch.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const {Transform} = require('stream');
  3. const withParser = require('./withParser');
  4. class Batch extends Transform {
  5. static make(options) {
  6. return new Batch(options);
  7. }
  8. static withParser(options) {
  9. return withParser(Batch.make, options);
  10. }
  11. constructor(options) {
  12. super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
  13. this._batchSize = 1000;
  14. if (options && typeof options.batchSize == 'number' && options.batchSize > 0) {
  15. this._batchSize = options.batchSize;
  16. }
  17. this._accumulator = [];
  18. }
  19. _transform(chunk, _, callback) {
  20. this._accumulator.push(chunk);
  21. if (this._accumulator.length >= this._batchSize) {
  22. this.push(this._accumulator);
  23. this._accumulator = [];
  24. }
  25. callback(null);
  26. }
  27. _flush(callback) {
  28. if (this._accumulator.length) {
  29. this.push(this._accumulator);
  30. this._accumulator = null;
  31. }
  32. callback(null);
  33. }
  34. }
  35. Batch.batch = Batch.make;
  36. Batch.make.Constructor = Batch;
  37. module.exports = Batch;