The EventEmitter is the observer pattern in disguise — the backbone of Node's events, the DOM, and most state libraries.
Map<string, Set<fn>> — O(1) add/remove and automatic dedupe of the same handler.off() (common with once) mutates the collection you're iterating.this so calls chain.once remove itself?" → the wrapper calls off before delegating.'*' listener list, or split on :.removeAllListeners(event) and document that callers must off.Build it, then add once without leaking the wrapper reference — the subtle part is removing the wrapper, not the original fn.
class EventEmitter { // your code here } const bus = new EventEmitter(); const fn = (x) => console.log('got', x); bus.on('data', fn); bus.emit('data', 42); // got 42 bus.off('data', fn); bus.emit('data', 99); // (nothing)
Test Code
Enter JavaScript that runs after your solution. It should return a value or a Promise.