-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathebus.js
More file actions
291 lines (235 loc) · 6.12 KB
/
ebus.js
File metadata and controls
291 lines (235 loc) · 6.12 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Bus - A generic event bus
*
* Use var myObj = new require("ebus")(); to inherit the functions
* Use myObj.on("event", callback) to attach event handlers
* Use emit("event", data, callback) to trigger the event handlers
*
* Each listener is called with two parameters, data and a function.
*
* Listeners are expected to call the function with no parameters
* if they are successful, and with an error object to stop the
* event from propagating to downstream listeners.
*
* Finally, the callback passed to emit itself will be called, either
* with no arguments, if all handlers were successful, or with
* the error object returned by the listener which failed.
*/
/* global performance */
/* eslint-env node */
/*
Debug levels
0 - None
1 - Definite problem, e.g. calling next twice
2 - Likely problem, e.g. thrown error
3 - Possible problem, e.g. error in callback
4 - Trace execution path through ebus
5 - Log performance
Each level includes all the messages from lower levels.
*/
require("setimmediate"); // polyfills the global setImmediate
function Ebus(p) {
"use strict";
this.debug = false;
this.yields = false;
this.handlers = {};
if (p) {
this.priorities = p;
} else {
this.priorities = {};
}
}
Ebus.prototype.on = function (event, p1, p2) {
"use strict";
var i, line = "", stack, handle,
len, callback, priority;
if (typeof p1 === "function") {
callback = p1;
priority = p2;
} else {
callback = p2;
priority = p1;
}
if (this.debug) {
stack = new Error().stack;
line = event + ":" + priority + " " + callback.name;
if (stack) {
line = line + "@" + stack.split("\n")[2].
replace(/^.*\//, "").
replace(/\:[^:]*$/, "");
}
}
if (typeof priority === "string") {
priority = this.priorities[priority];
}
if (typeof priority !== "number") {
priority = 0;
// throw new Error("EBUS_INVALID_PRIORITY");
}
if (typeof callback !== "function") {
throw new Error("EBUS_INVALID_LISTENER");
}
if (!this.handlers[event]) {
this.handlers[event] = [];
}
len = this.handlers[event].length;
handle = {
fn: callback,
async: callback.length >= 2,
priority: priority,
line: line
};
if (!len || priority <= this.handlers[event][len - 1].priority) {
this.handlers[event].push(handle);
} else {
for(i = len - 1; i >= 0; i--) {
if(this.handlers[event][i].priority >= priority) { break; }
}
this.handlers[event].splice(i + 1, 0, handle);
}
};
Ebus.prototype.off = function(event, cb) {
"use strict";
var i, l;
if (this.handlers[event]) {
for (i = 0, l = this.handlers[event].length; i < l; i++) {
if (this.handlers[event][i].fn === cb) {
this.handlers[event].splice(i, 1);
break;
}
}
}
};
Ebus.prototype.emit = function(event, data, cb) {
"use strict";
var listeners = this.handlers[event] || [],
status = {},
i = 0,
runningCount = 0,
calledBack = false,
li,
prio,
debug = this.debug,
timer,
RUNNING = 1,
SUCCESS = 2,
ERROR = 3;
if (!listeners) {
if (cb) { cb(null, data); }
return;
}
function success () {
if(timer) { clearTimeout(timer); }
calledBack = true;
if(cb) {
setImmediate(function () { cb(null, data); });
}
return;
}
function error(err) {
if(timer) { clearTimeout(timer); }
calledBack = true;
if (cb) {
setImmediate(function () { cb(err, data); });
}
return;
}
function dumpStatus() {
for(var j in status) {
console.log(listeners[j].line + " -- " + ["Running", "Success", "Error"][status[j] - 1]);
}
}
function getNext(prevIx) {
var start;
if(typeof prevIx !== "undefined") { // Skip when invoking first listener
status[prevIx] = RUNNING;
runningCount++;
if(debug) {
start = process && process.hrtime ? process.hrtime() : performance ? performance.now() : Date.now();
}
}
return function (err) {
if(typeof prevIx !== "undefined") { // Skip when invoking first listener
if(debug > 4) {
console.log(listeners[prevIx].line + " called next after " + (
process && process.hrtime ? process.hrtime(start)[1] / 1000000 : (performance ? performance.now() : Date.now()) - start
));
}
if(calledBack) {
if(debug > 0) { console.log("Next() after emit called back" + listeners[prevIx].line); }
return;
}
if(status[prevIx] !== RUNNING) {
if(debug > 0) { console.log("Multiple next() from " + listeners[prevIx].line); }
throw Error("EBUS_MULTIPLE_NEXT");
}
if (err) {
status[prevIx] = ERROR;
if(debug > 2) { console.log("Received error from " + listeners[prevIx].line + ":\n", err); }
error(err);
return;
}
status[prevIx] = SUCCESS;
if(runningCount > 0) { runningCount--; }
if(prevIx === i) {
if(debug > 4) { console.log("Sync callback from " + listeners[i].line + "; if possible, remove callback for better perf."); }
return;
}
if(runningCount > 0) { return; }
}
prio = listeners[i] && listeners[i].priority;
while(true) {
if (i >= listeners.length) {
if (runningCount === 0) {
success(data);
}
return;
}
li = listeners[i];
if(li.priority !== prio) {
if(runningCount > 0) {
// if an async listener has been fired, wait for it to be done.
break;
} else {
// otherwise, bump the prio and keep looping
prio = li.priority;
}
}
if (debug > 3) { console.log("Calling " + li.line); }
if(li.async) {
li.fn.call(null, data, getNext(i));
} else {
li.fn.call(null, data);
}
i++;
}
};
}
if (debug > 0) {
timer = setTimeout(function () {
console.log("Emit timed out. Status:");
dumpStatus();
}, 5000);
}
getNext()();
};
Ebus.prototype.setDebug = function(level) {
"use strict";
if(level === false) {
level = 0;
} else if(level === true) {
level = 5;
}
this.debug = level;
};
Ebus.prototype.setYields = function(flag) {
"use strict";
console.log("Yields is not yet supported");
this.yields = flag;
};
Ebus.prototype.dump = function (event) {
"use strict";
console.log(this.handlers[event].map(function (handler) {
return handler.line;
}).join("\n"));
};
module.exports = Ebus;