class EventEmitter {
#handlers = new Map();
on(event, handler) {
if (!this.#handlers.has(event)) {
this.#handlers.set(event, new Set());
}
this.#handlers.get(event).add(handler);
return this;
}
emit(event, ...args) {
const handlers = this.#handlers.get(event);
if (handlers) {
handlers.forEach(h => h(...args));
}
}
}
const emitter = new EventEmitter();
const fn = (x) => console.log(x);
emitter.on('test', fn).on('test', fn);
emitter.emit('test', 'hello'); 