class Emitter { #listeners; constructor() { this.#listeners = new Map(); } /** * Registers a callback function for the specified event. * * @param {string} event - The name of the event. * @param {Function} callback - The callback function to be executed when the event is triggered. */ on(event, callback) { if (!this.#listeners.has(event)) { this.#listeners.set(event, new Set()); } this.#listeners.get(event).push(callback); } /** * Removes an event listener callback for the specified event. * If no callback is provided, removes all callbacks for the event. * * @param {string} event - The name of the event. * @param {Function} [callback] - The callback function to be removed. */ off(event, callback = undefined) { if (typeof event != 'string') return; if (!this.#listeners.has(event)) return false; if (!callback || this.#listeners.get(event).size === 0) { return this.#listeners.delete(event); } this.#listeners.get(event).delete(callback); } /** * Emits an event and invokes all registered listeners for that event. * * @param {string} event - The name of the event to emit. * @param {...any} args - The arguments to pass to the event listeners. */ emit(event, ...args) { if (!this.#listeners.has(event)) { return; } for (const callback of [...this.#listeners.get(event)]) { callback(this, ...args); } } } export default Emitter;