-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.js
70 lines (58 loc) · 1.68 KB
/
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
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
69
70
/**
* @file Defines a global event bus. Include this file before others.
* @author Michael Pascale
*/
/**
* Create an event bus.
* @constructor
*/
function EventBus () {
this.events = new Map();
/**
* Register an event handler.
* @method
* @param {String} event - The name of the event to listen to.
* @param {Function} handler - A function to call when the event occurs.
*/
this.register = function (event, handler) {
if (!this.events.has(event))
this.events.set(event, []);
this.events.get(event).push(handler);
};
/**
* Unregister an event handler.
* @method
* @param {String} event - The name of the event.
* @param {Function} handler - A function to remove from the event.
*/
this.unregister = function (event, handler) {
if (this.events.has(event)) {
const queue = this.events.get(event);
if (queue.length < 1) {
this.events.delete(event);
} else {
queue.splice(queue.indexOf(handler), 1);
}
}
};
/**
* Trigger an event.
* @method
* @param {String} event - The name of the event to trigger.
* @param {*} args - Arguments to be supplied to each handler.
*/
this.emit = function (event, ...args) {
// if (DEV_MODE)
// console.log(`Event: ${event}`);
if (this.events.has(event)) {
const queue = this.events.get(event);
for (const handler of queue) {
handler(...args);
}
}
};
}
self.globalBus = new EventBus();
window.onmousemove = function (ev) {
window.MOUSE_POS = ev;
};