-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (27 loc) · 803 Bytes
/
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
module.exports = {
subscribers: {},
on: function(event, cb) {
(this.subscribers[event] = this.subscribers[event] || []).push(cb);
return cb;
},
off: function(id) {
for (var event in this.subscribers) {
var event_subscribers = this.subscribers[event];
for (var i = 0; i < event_subscribers.length; i++) {
if (event_subscribers[i] === id) {
event_subscribers.splice(i, 1);
}
}
}
},
all: function(callback) {
return this.on("*", callback);
},
send: function(event, data, source) {
var event_subscribers =
(this.subscribers[event] || []).concat(this.subscribers["*"] || []);
for (var i = 0; i < event_subscribers.length; i++) {
event_subscribers[i](event, data, source, event_subscribers);
}
}
};