Loading problem…
This problem builds on event-emitter-i. Complete that first, then load your solution to continue.
Implement an EventEmitter class with on, off, once, and emit methods.
This version builds on the basic EventEmitter by adding:
1) once(event, handler) — handler runs at most once.
2) emit(event, ...args) returns an array of handler return values.
on(event, handler)this (chainable).once(event, handler)this.off(event, handler)this.emit(event, ...args)...args to each handler.once handlers after they run.const ee = new EventEmitter();
const calls = [];
ee.on('a', (x) => { calls.push('on'); return x + 1; });
ee.once('a', () => { calls.push('once'); return 99; });
const r1 = ee.emit('a', 1); // ['on' return, 'once' return] => [2, 99]
const r2 = ee.emit('a', 1); // once removed => [2]
// calls => ['on', 'once', 'on']