File size: 866 Bytes
7ffaa9e |
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 |
export class EventEmitter {
constructor() {
this.listeners = new Map();
}
on(event, callback) {
if (!this.listeners.has(event)) {
this.listeners.set(event, new Set());
}
this.listeners.get(event).add(callback);
}
off(event, callback) {
if (this.listeners.has(event)) {
this.listeners.get(event).delete(callback);
}
}
emit(event, ...args) {
if (this.listeners.has(event)) {
this.listeners.get(event).forEach(callback => callback(...args));
}
}
clear() {
this.listeners.clear();
}
}
export const EventTypes = {
KEY_DOWN: 'keydown',
KEY_UP: 'keyup',
KEY_PRESS: 'keypress',
MOUSE_MOVE: 'mousemove',
MOUSE_DOWN: 'mousedown',
MOUSE_UP: 'mouseup',
MOUSE_WHEEL: 'wheel',
CLICK: 'click'
};
|