Initial commit
This commit is contained in:
44
themes/keepit/node_modules/batch-stream/lib/index.js
generated
vendored
Normal file
44
themes/keepit/node_modules/batch-stream/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
var stream = require('readable-stream')
|
||||
, util = require('util');
|
||||
|
||||
|
||||
module.exports = BatchStream;
|
||||
|
||||
|
||||
function BatchStream (options) {
|
||||
if (!(this instanceof BatchStream)) return new BatchStream(options);
|
||||
|
||||
options || (options = {});
|
||||
|
||||
var transformOptions = {
|
||||
objectMode: true
|
||||
}
|
||||
if(options.highWaterMark !== undefined) {
|
||||
transformOptions.highWaterMark = options.highWaterMark
|
||||
}
|
||||
|
||||
stream.Transform.call(this, transformOptions);
|
||||
this.size = options.size || 100;
|
||||
this.batch = [];
|
||||
}
|
||||
util.inherits(BatchStream, stream.Transform);
|
||||
|
||||
|
||||
BatchStream.prototype._transform = function (chunk, encoding, callback) {
|
||||
this.batch.push(chunk);
|
||||
if (this.batch.length >= this.size) {
|
||||
this.push(this.batch);
|
||||
this.batch = [];
|
||||
}
|
||||
callback();
|
||||
};
|
||||
|
||||
|
||||
BatchStream.prototype._flush = function (callback) {
|
||||
if (this.batch.length) {
|
||||
this.push(this.batch);
|
||||
this.batch = [];
|
||||
}
|
||||
callback();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user