game-engine/modules/event-emitter.js

28 lines
532 B
JavaScript
Raw Permalink Normal View History

2024-09-17 23:16:36 -05:00
export class EventEmitter {
static instance;
2024-09-17 23:16:36 -05:00
events;
constructor() {
2024-09-17 23:16:36 -05:00
if (!EventEmitter.instance) {
EventEmitter.instance = this;
this.events = {};
}
2024-09-17 23:16:36 -05:00
return EventEmitter.instance;
}
on(eventName, callback) {
2024-09-17 23:16:36 -05:00
if (!this.events[eventName]) {
this.events[eventName] = [];
}
2024-09-17 23:16:36 -05:00
this.events[eventName].push(callback);
}
emit(eventName, ...args) {
2024-09-17 23:16:36 -05:00
if (this.events[eventName]) {
this.events[eventName].forEach((callback) => {
callback(...args);
});
}
}
}