game-engine/modules/event-emitter.js

27 lines
532 B
JavaScript

export class EventEmitter {
static instance;
events;
constructor() {
if (!EventEmitter.instance) {
EventEmitter.instance = this;
this.events = {};
}
return EventEmitter.instance;
}
on(eventName, callback) {
if (!this.events[eventName]) {
this.events[eventName] = [];
}
this.events[eventName].push(callback);
}
emit(eventName, ...args) {
if (this.events[eventName]) {
this.events[eventName].forEach((callback) => {
callback(...args);
});
}
}
}