Object-Oriented / Patterns
Custom Event Emitter
mediumJS / TS
Problem Statement
Design an `EventEmitter` class with `subscribe(eventName, callback)` and `emit(eventName, args)` methods. `subscribe` should return an unsubscribe handle object with an `unsubscribe()` method.
Examples
Example:
const emitter = new EventEmitter();
const sub = emitter.subscribe('onClick', (x) => console.log(x));
emitter.emit('onClick', [42]); // prints 42
sub.unsubscribe();Includes TypeScript & JavaScript starter code
Start Interview with this Problem