-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDispatcher.js
54 lines (47 loc) · 1.46 KB
/
EventDispatcher.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
'use strict';
/**
* By Pipo From three.js, in turn form https://github.com/mrdoob/eventdispatcher.js/
*/
class EventDispatcher {
addEventListener(type, listener) {
if (this._listeners === undefined) this._listeners = {};
const listeners = this._listeners;
if (listeners[type] === undefined) {
listeners[type] = [];
}
if (listeners[type].indexOf(listener) === -1) {
listeners[type].push(listener);
}
}
hasEventListener(type, listener) {
if (this._listeners === undefined) return false;
const listeners = this._listeners;
return listeners[type] !== undefined &&
listeners[type].indexOf(listener) !== -1;
}
removeEventListener(type, listener) {
if (this._listeners === undefined) return;
const listeners = this._listeners;
const listenerArray = listeners[type];
if (listenerArray !== undefined) {
const index = listenerArray.indexOf(listener);
if (index !== -1) {
listenerArray.splice(index, 1);
}
}
}
dispatchEvent(event) {
if (this._listeners === undefined) return;
const listeners = this._listeners;
const listenerArray = listeners[event.type];
if (listenerArray !== undefined) {
event.target = this;
// Make a copy, in case listeners are removed while iterating.
const array = listenerArray.slice(0);
for (let i = 0, l = array.length; i < l; i++) {
array[i].call(this, event);
}
event.target = null;
}
}
}