-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.js
More file actions
35 lines (31 loc) · 801 Bytes
/
events.js
File metadata and controls
35 lines (31 loc) · 801 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
29
30
31
32
33
34
35
// --- Directions
// Create an 'eventing' library out of the
// Events class. The Events class should
// have methods 'on', 'trigger', and 'off'.
class Events {
constructor(){
this.events = {};
}
// Register an event handler
on(eventName, callback) {
if(this.events[eventName]) {
this.events[eventName].push(callback);
} else {
this.events[eventName] = [callback]
}
}
// Trigger all callbacks associated
// with a given eventName
trigger(eventName) {
if (this.events[eventName]){
for ( let cb of this.events[eventName]){
cb();
}
}
}
// Remove all event handlers associated
// with the given eventName
off(eventName) {
delete this.events[eventName]
}
}