Deviopath › JavaScript

Event Emitter

Hard · Data Structures

Finish the EventEmitter class.

- on(event, listener) subscribes a listener and returns a function that unsubscribes it. Calling that function more than once is safe.

- once(event, listener) works like on, but the listener is removed right before its first call. It also returns an unsubscribe function.

- emit(event, ...args) calls every listener of event with args, in the order they subscribed, and returns how many listeners were called (0 if none).

The function runEmitter is already written. For ["on", event, name] it subscribes a listener that logs name(args), ["off", event, name] calls the unsubscribe function for name, and after each ["emit", event, ...args] it logs "=" + count. It returns the log.

Next: Debounce With a Fake Clock