-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathevent.js
More file actions
28 lines (24 loc) · 838 Bytes
/
event.js
File metadata and controls
28 lines (24 loc) · 838 Bytes
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
class Event {
constructor() {
this.listeners = [];
this.keyedListeners = {};
}
// Attaches a listener for the lifetime of the object containing the event.
// These listeners cannot be detached.
attach(listener) {
this.listeners.push(listener);
}
// Attaches a listener with a specific key, which can be detached
attachKeyed(key, listener) {
this.keyedListeners[key] = listener;
}
// Detaches the listener with the specified key.
detachKeyed(key) {
delete this.keyedListeners[key];
}
// args (if any) is expected to be a dictionary of key-value pairs.
fire(sender, args) {
this.listeners.forEach(listener => listener(sender, args));
Object.values(this.keyedListeners).forEach(listener => listener(sender, args));
}
}