This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathemit.js
More file actions
executable file
·68 lines (57 loc) · 1.91 KB
/
emit.js
File metadata and controls
executable file
·68 lines (57 loc) · 1.91 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
var test = require('tap').test;
var EventEmitter = require('events').EventEmitter;
var dnode = require('../');
test('emit events', function (t) {
t.plan(2);
var port = Math.floor(1e4 + (Math.random() * 5e4 - 1e4));
var subs = [];
function publish (name) {
var args = [].slice.call(arguments, 1);
subs.forEach(function (sub) {
sub.emit(name, args);
});
}
var server = dnode(function (remote, conn) {
this.subscribe = function (emit) {
subs.push({ emit : emit, id : conn.id });
conn.on('end', function () {
for (var i = 0; i < subs.length; i++) {
if (subs.id === conn.id) {
subs.splice(i, 1);
break;
}
}
});
};
}).listen(port);
server.on('ready', function () {
setTimeout(function () {
var iv = setInterval(function () {
publish('data', Math.floor(Math.random() * 100));
}, 20);
server.on('close', function () {
clearInterval(iv);
});
}, 20);
var xs = [];
var x = dnode.connect(port, function (remote) {
var em = new EventEmitter;
em.on('data', function (n) { xs.push(n) });
remote.subscribe(em.emit.bind(em));
});
var ys = [];
var y = dnode.connect(port, function (remote) {
var em = new EventEmitter;
em.on('data', function (n) { ys.push(n) });
remote.subscribe(em.emit.bind(em))
});
setTimeout(function () {
t.ok(xs.length > 5);
t.deepEqual(xs, ys);
x.end();
y.end();
server.close();
t.end();
}, 200);
});
});