Proof of Work · Challenge
Subscribe, unsubscribe, and stay safe while it is all happening.
about 20 min · 6 required tests · in the browser
What is recorded: when you save, your Result page shows your code, which tests passed, how many times you ran the tests, how many versions you tried, the time from first run to save, and the largest jump in code between runs. Anyone with the link can see it. The suggested time is not enforced.
Most front-end code ends up with a small pub/sub: a store tells the UI something changed, a socket tells the app a message arrived. Build the core of one.
Implement Emitter, a class with these methods:
on(event, fn) registers fn for event.once(event, fn) registers fn, but it runs at most one time.off(event, fn) removes that listener. Removing a listener that was never added does nothing.emit(event, ...args) calls every listener for event, in the order they were added, passing args to each.A listener can call off while emit is running, even on itself. Every listener that was registered when emit started still runs, once, and none of them is skipped.
const bus = new Emitter()
bus.on("save", (name) => console.log("saved", name))
bus.once("save", () => console.log("first save only"))
bus.emit("save", "draft") // logs "saved draft", then "first save only"
bus.emit("save", "final") // logs "saved final"Requirements:
emit reaches the listener.off removes only the listener you name.Write a top-level Emitter that works with new. A class or a constructor function both work. Do not use export or module.exports.
Press Esc, then Tab, to leave the editor.
⌘ / Ctrl + Enter runs the tests
Hiring developers? See how teams use this.
Graded on
0/6
Other builds