interval.js
877 Bytes
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
var binary = require('../');
var test = require('tap').test;
var EventEmitter = require('events').EventEmitter;
test('interval', function (t) {
t.plan(1);
var em = new EventEmitter;
var i = 0;
var iv = setInterval(function () {
var buf = new Buffer(1000);
buf[0] = 0xff;
if (++i >= 1000) {
clearInterval(iv);
buf[0] = 0;
}
em.emit('data', buf);
}, 1);
var loops = 0;
binary(em)
.loop(function (end) {
this
.word8('x')
.word8('y')
.word32be('z')
.word32le('w')
.buffer('buf', 1000 - 10)
.tap(function (vars) {
loops ++;
if (vars.x == 0) end();
})
})
.tap(function () {
t.same(loops, 1000);
})
;
});