JavaScript utilities · Senior · about 25 minutes
Event emitter
Implement class EventEmitter with on(event, listener), off(event, listener), once(event, listener) and emit(event, ...args). on returns a function that removes that listener. emit calls the listeners in the order they were added and returns true when there was at least one. A listener removed while an emit is running still gets that emit; one added during an emit waits for the next. In the tests a listener named A logs [A, ...args]; A!B also removes B; A+C also adds C.
Start from
class EventEmitter {
on(event, listener) {
// Add the listener and return a function that removes it.
}
off(event, listener) {}
once(event, listener) {}
emit(event, ...args) {}
}Hints and solution
Solve it in the app: its tests run without a year.
The hints, the solution and what an interviewer looks for opens with a year in the app. What a year includes