This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (140 loc) · 4.3 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var util = require('util');
var events = require('events');
var uuid = require('node-uuid');
// Alias for quick access.
var arraySlice = Array.prototype.slice;
// Helper used to parse transports from an arguments list. Returns an array.
var transportArgs = function(args) {
var transports = args[0];
if (!Array.isArray(transports))
transports = arraySlice.call(args, 0);
return transports;
};
function Redub() {
var self = this;
var idsSeen = Object.create(null);
var timeout;
var interval = null;
// Handler function for incoming messages. This will emit a `message` event.
this.messageHandler = function(msg) {
var id = msg[0];
var payload = msg[1];
if (idsSeen[id] !== undefined)
return;
idsSeen[id] = Date.now();
self.emit('message', payload, id);
};
// Periodically clean up the index of IDs.
var expireHandler = function() {
var max = Date.now() - timeout;
for (var key in idsSeen) {
if (idsSeen[key] <= max) {
delete idsSeen[key];
}
}
};
// Control lifespan of IDs, used to detect duplicates.
Object.defineProperty(this, 'timeout', {
get: function() {
return timeout;
},
set: function(value) {
timeout = value;
if (interval)
clearInterval(interval);
if (timeout > 0)
interval = setInterval(expireHandler, timeout);
else
interval = null;
}
});
this.transports = [];
this.uid = exports.defaultUid;
this.timeout = exports.defaultTimeout;
}
util.inherits(Redub, events.EventEmitter);
// Add one or more transports. Transports can be provided as an array, as
// separate arguments or as a method chain.
//
// redub.add([t1, t2, t3]);
// redub.add(t1, t2, t3);
// redub.add(t1).add(t2).add(t3);
//
Redub.prototype.add = function() {
var transports = this.transports;
var messageHandler = this.messageHandler;
transportArgs(arguments).forEach(function(transport) {
var idx = transports.indexOf(transport);
if (idx === -1) {
transport.addListener('message', messageHandler);
transports.push(transport);
}
});
return this;
};
// Remove one or more transports. Transports can be provided as an array, as
// separate arguments or as a method chain.
//
// redub.remove([t1, t2, t3]);
// redub.remove(t1, t2, t3);
// redub.remove(t1).remove(t2).remove(t3);
//
Redub.prototype.remove = function() {
var transports = this.transports;
var messageHandler = this.messageHandler;
transportArgs(arguments).forEach(function(transport) {
var idx = transports.indexOf(transport);
if (idx !== -1) {
transport.removeListener('message', messageHandler);
transports.splice(idx, 1);
}
});
return this;
};
// Reset the list of transports. Transports can be provided as an array, as
// separate arguments or as a method chain.
//
// redub.reset([t1, t2, t3]);
// redub.reset(t1, t2, t3);
//
Redub.prototype.reset = function() {
var messageHandler = this.messageHandler;
this.transports.forEach(function(transport) {
transport.removeListener('message', messageHandler);
});
this.transports = [];
this.add(transportArgs(arguments));
return this;
};
// Send a message over all transports. Returns the message ID.
Redub.prototype.send = function(msg) {
var id = this.uid(msg);
msg = [id, msg];
this.transports.forEach(function(transport) {
transport.send(msg);
});
return id;
};
// Detach from all transports and stop processing.
Redub.prototype.end = function() {
this.timeout = 0;
this.reset();
};
// Create a Redub instance, with an initial set of transports. Transports can
// be provided as an array or as separate arguments.
//
// var redub = require('redub');
// var chan1 = redub([t1, t2, t3]);
// var chan2 = redub(t1, t2, t3);
//
exports = module.exports = function() {
var channel = new Redub();
channel.add(transportArgs(arguments));
return channel;
};
// Default ID generator.
exports.defaultUid = function(msg) {
return uuid();
};
// Default timeout.
exports.defaultTimeout = 10000;