-
Notifications
You must be signed in to change notification settings - Fork 3
/
event.js
36 lines (30 loc) · 863 Bytes
/
event.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
module.exports = Event
function Event() {
var listeners = []
return { broadcast: broadcast, listen: event }
function broadcast(value) {
var listenersCopy = listeners.slice()
for (var i = 0; i < listenersCopy.length; i++) {
if (!listenersCopy[i].deleted) {
listenersCopy[i].fn(value)
}
}
}
function event(listener) {
listeners.push(new ListItem(listener))
return removeListener
function removeListener() {
for (var i = 0; i < listeners.length; i++) {
if (listeners[i].fn === listener) {
listeners[i].deleted = true
listeners.splice(i, 1)
break
}
}
}
}
}
function ListItem(fn) {
this.fn = fn
this.deleted = false
}