index.js
1.21 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
/**
* @constructor
*/
var EmitterSteward = function(emitter) {
this.currentEmitter = null;
this.counter = 0;
this.debug = function (msg, vars) {
emitter.emit("msg:debug", {msg: msg, vars: vars});
};
this.setWatcher(1000);
};
/**
* @param timeout
*/
EmitterSteward.prototype.setWatcher = function (timeout) {
var that = this;
this._int = setInterval(function () {
that.currentEmitter = null;
}, timeout);
};
/**
* Clear the interval
*/
EmitterSteward.prototype.destroy = function () {
if (this._int) {
return clearInterval(this._int);
}
};
/**
* @param id
* @returns {boolean}
*/
EmitterSteward.prototype.valid = function (id) {
var counter = this.counter += 1;
var debug = this.debug;
if (!this.currentEmitter) {
this.currentEmitter = id;
debug("%s:Setting current emitter:", counter);
return true;
} else {
if (id === this.currentEmitter) {
debug("%s:Same emitter, allowing event", counter);
return true;
} else {
debug("%s:Emitter set, but a different one, refusing", counter);
return false;
}
}
};
module.exports = EmitterSteward;