3d-arena / viewer /src /lib /splat.js /core /EventDispatcher.ts
dylanebert's picture
dylanebert HF Staff
reformat export
5f32ba4
raw
history blame
1.24 kB
class EventDispatcher {
listeners: { [key: string]: Function[] } = {};
addEventListener(type: string, listener: Function): void {
if (this.listeners[type] === undefined) {
this.listeners[type] = [];
}
if (this.listeners[type].indexOf(listener) === -1) {
this.listeners[type].push(listener);
}
}
hasEventListener(type: string, listener: Function): boolean {
return this.listeners[type] !== undefined && this.listeners[type].indexOf(listener) !== -1;
}
removeEventListener(type: string, listener: Function): void {
const listenerArray = this.listeners[type];
if (listenerArray !== undefined) {
const index = listenerArray.indexOf(listener);
if (index !== -1) {
listenerArray.splice(index, 1);
}
}
}
dispatchEvent(event: { type: string }): void {
const listenerArray = this.listeners[event.type];
if (listenerArray !== undefined) {
const array = listenerArray.slice(0);
for (let i = 0, l = array.length; i < l; i++) {
array[i].call(this, event);
}
}
}
}
export { EventDispatcher };