loop.js
2.07 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var Seq = require('seq');
var Hash = require('hashish');
var EventEmitter = require('events').EventEmitter;
var Bin = require('binary');
var Buf = require('bufferlist/binary');
var BufferList = require('bufferlist');
console.log('loop');
function emitter () {
var em = new EventEmitter;
var i = 0;
var iv = setInterval(function () {
var buf = new Buffer(10000);
buf[0] = 0xff;
if (++ i >= 2000) {
buf[0] = 0;
clearInterval(iv);
}
em.emit('data', buf);
}, 1);
return em;
}
Seq()
.seq(function () {
var next = this.bind({}, null);
bufferlist(next);
})
.seq(function () {
var next = this.bind({}, null);
binary(next);
})
;
function binary (next) {
var em = emitter();
var t0 = Date.now();
Bin(em)
.loop(function (end) {
this
.word8('x')
.word8('y')
.word32be('z')
.word32le('w')
.buffer('buf', 10000 - 10)
.tap(function (vars) {
if (vars.x === 0) {
var tf = Date.now();
console.log(' binary: ' + (tf - t0) + ' ms');
end();
setTimeout(next, 20);
}
})
})
;
}
function bufferlist (next) {
var em = emitter();
var t0 = Date.now();
var blist = new BufferList;
em.on('data', function (buf) {
blist.push(buf);
});
Buf(blist)
.forever(function () {
var top = this;
this
.getWord8('x')
.getWord8('y')
.getWord32be('z')
.getWord32le('w')
.getBuffer('buf', 10000 - 10)
.tap(function (vars) {
if (vars.x === 0) {
var tf = Date.now();
console.log(' bufferlist: ' + (tf - t0) + ' ms');
top.exit();
setTimeout(next, 20);
}
})
})
.end()
;
}