-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.js
48 lines (37 loc) · 978 Bytes
/
train.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
var EventEmitter = require("events").EventEmitter;
var util = require("util");
function train(functions) {
EventEmitter.call(this);
this._queue = [];
this._queing = false;
var self = this;
for(var p in functions) {
if(p[0] !== "_" && typeof functions[p] === "function") {
this[p] = (function(func) {
return function wrapper(arg1, arg2, arg3) {
if(self._queing) {
this._queue.push([wrapper, arg1, arg2, arg3]);
return this;
} else {
return func.call(self, arg1, arg2, arg3);
}
};
})(functions[p]);
}
}
return this;
}
util.inherits(train, EventEmitter);
train.prototype._startQueing = function() {
this._queing = true;
return this;
},
train.prototype._stopQueing = function() {
this._queing = false;
var q;
while(!this._queing && (q = this._queue.shift())) {
q[0].call(this, q[1], q[2], q[3]);
}
return this;
}
module.exports = train;