Skip to curriculum
Jump to the editor

19.1. An event emitter

Medium
Topics
20–35 min

A chat client has one socket and many parts of the page that care about it: the message list, the unread badge, the typing indicator. None of them should know about the others. An event emitter sits in the middle: parts subscribe to a named event, and whoever owns the socket announces the event once. Every listener hears it, in order.

Write the class EventEmitter in emitter.js.

Signature

JAVASCRIPT
class EventEmitter {
  on(name, listener)     // returns this
  once(name, listener)   // returns this
  off(name, listener)    // returns this
  emit(name, ...args)    // returns true or false
  listenerCount(name)    // returns a number
}
  • name: any string, including names such as "constructor" or "__proto__".
  • listener: a function. It is called with the arguments given to emit, and this is the emitter.

Requirements

  1. emit(name, ...args) calls every listener of name with all of args, in the order the listeners were added, with this set to the emitter.
  2. emit returns true when name had at least one listener, and false otherwise.
  3. Events are separate by name, and no name is special: "constructor" has no listeners until someone adds one.
  4. on, once and off return the emitter, so calls can be chained.
  5. Each on is its own registration: the same function added twice runs twice.
  6. off(name, listener) removes one registration of listener, the most recently added one. An unknown event or listener changes nothing.
  7. once(name, listener) registers a listener for the next emit only. off(name, listener) with the same function removes it before it runs.
  8. A once listener is removed before it is called, and it never runs twice, even when the listener (or another listener) emits the same event again.
  9. emit calls the listeners that were registered when it started. A listener added during an emit first runs in the next one; a listener removed during an emit still runs in that one, and not after.
  10. listenerCount(name) returns the number of registrations of name, on and once alike.
  11. on and once throw a TypeError when listener is not a function, and register nothing.
  12. Every emitter has its own listeners.

Examples

JAVASCRIPT
const chat = new EventEmitter();
const show = (from, text) => console.log(from + ": " + text);

chat.on("message", show).once("message", () => console.log("first message!"));
chat.emit("message", "Asha", "hi");  // "Asha: hi", then "first message!"
chat.emit("message", "Ben", "hey");  // "Ben: hey"
chat.off("message", show);
chat.emit("message", "Asha", "?");   // false: nobody listens
JAVASCRIPT
const e = new EventEmitter();
const a = () => console.log("a");
e.on("x", a).on("x", () => console.log("b")).on("x", a);
e.off("x", a);       // removes the second a, the most recent one
e.emit("x");         // "a", then "b"
JAVASCRIPT
const e = new EventEmitter();
e.on("x", () => {
  console.log("first");
  e.on("x", () => console.log("late"));
});
e.emit("x");   // "first" only: "late" was added during this emit
e.emit("x");   // "first", "late"

Environment

  • Plain JavaScript in a browser page; the tests create emitters with new EventEmitter().
  • Errors thrown by a listener are not part of this problem: they may stop the emit.
  • Wildcard events, prependListener and limits on the number of listeners are not part of this problem.

Hints

  1. If emit loops over an array and a listener adds or removes a registration, what happens to the loop when you change that same array?
  2. How can off find a once listener when the caller only has the function they passed in?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…