-
Notifications
You must be signed in to change notification settings - Fork 3
/
arrayBuffer.js
45 lines (39 loc) · 1.06 KB
/
arrayBuffer.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
var ArrayBuffer = function(_name, _output) {
this.name = _name;
this.buffer = [];
this.bufferLength = 100;
this.pushedFirstItem = false;
this.output = _output;
this.flushCount = 0;
this.push = function(value) {
this.buffer.push(value);
if (this.buffer.length === this.bufferLength) {
this.flush();
}
};
this.flush = function() {
if (this.buffer.length) {
this.flushCount++;
if (this.pushedFirstItem) {
this.output.write(',');
this.output.write(this.buffer.join(','));
}
else {
this.output.write(this.name + ':[');
this.output.write(this.buffer.join(','));
}
this.pushedFirstItem = true;
this.buffer.length = 0;
}
};
this.end = function(comma) {
this.flush();
if (comma) {
this.output.write('],');
}
else {
this.output.write(']');
}
};
};
module.exports = ArrayBuffer;