Loading problem…
This problem builds on event-emitter. Complete that first, then load your solution to continue.
Extend EventEmitter with once semantics while keeping dispatch deterministic under mutation.
This is an API-contract problem, not a syntax problem. Interviewers are testing whether you can preserve event ordering and correctness even when listeners are removed during emit.
Implement:
class EventEmitter {
on(event, handler) {}
once(event, handler) {}
off(event, handler) {}
emit(event, ...args) {}
}on, once, and off must return this for chaining.once(event, handler) registers a listener that can run at most once.off(event, handler) removes all listeners (including once listeners) registered with that handler.emit(event, ...args) executes listeners in registration order and returns an array of return values in that same order.emit must iterate over a snapshot of listeners for that event. If listeners are removed while dispatch is running (including once self-removal), all listeners that were scheduled at the start of the emit must still run exactly once in order.
once listener must be removable via off(event, handler) before its first execution.emit must not skip or reorder remaining listeners in that cycle.once listener must never execute more than once, even under nested or re-entrant emits.const ee = new EventEmitter();
ee.on('sum', (a, b) => a + b);
ee.once('sum', (a, b) => a * b);
ee.emit('sum', 2, 3); // [5, 6]
ee.emit('sum', 2, 3); // [5]