-
Notifications
You must be signed in to change notification settings - Fork 3
/
through2-batch.js
76 lines (65 loc) · 1.81 KB
/
through2-batch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Like through2 except execute in batches (with a size)
var through2 = require('through2');
module.exports = function batchThrough(options, transform, flush) {
var batched = [];
var batchSize;
var lastEnc = null;
if (typeof options !== 'object') {
flush = transform;
transform = options;
options = {};
}
if (typeof flush !== 'function') {
flush = function (callback) {
callback();
};
}
batchSize = options.batchSize || 10;
function _transform(message, enc, callback) {
var self = this;
lastEnc = enc;
batched.push(message);
if (batched.length < batchSize) {
callback();
} else if (transform) {
transform.call(this, batched, enc, function (err, data) {
batched = [];
callback(err, data);
});
} else {
callback(null, batched);
batched = [];
}
}
function _flush(callback) {
var self = this;
if (batched.length > 0) {
if (transform) {
transform.call(this, batched, lastEnc, function (err) {
batched = [];
if (err) callback(err);
else flush.call(self, callback);
});
} else {
callback(null, batched);
batched = [];
flush.call(this,callback);
}
} else {
flush.call(this,callback);
}
}
return through2(options, _transform, _flush);
};
module.exports.obj = function (options, transform, flush) {
if (typeof options !== 'object') {
flush = transform;
transform = options;
options = {};
}
options.objectMode = true;
if (options.highWaterMark === null) {
options.highWaterMark = 16;
}
return module.exports(options, transform, flush);
};