Build a Machine That Emits Events

In the last video, washer 1 was a plain EventEmitter, and your own code called .emit() on it from a timer. That showed how events work, but it isn’t how a real machine behaves. A real washer knows when its cycle ends, and it says so itself. With three machines in the laundry room at 14 Ferris Road, you don’t want every script to fake each machine’s cycle.

In this lesson, you’ll write a Washer class that announces "done" with the details its listeners need and reports an "error" when someone leaves the door open. You’ll also write listeners that stop listening once their owner is done with them. Start by giving the washer a class of its own.

Turn the Washer Into a Class

The washer in the video needed help from outside. A timer in the script stood in for its cycle, and the script called .emit(). Once you move the timer and the .emit() call inside the washer, each machine runs its own cycle and announces its own result, while the rest of the program only listens.

You already have the pieces. A class can build on another class with extends, which gives it every method of the parent, and its constructor calls super() before it uses this. If either is hazy, then Inheritance, Composition and JavaScript’s Types covers both.

A class that extends EventEmitter inherits .on(), .emit(), and the rest, so every washer it makes is an emitter. Save this class in your laundry folder as washer.mjs:

Language: JavaScript Filename: washer.mjs
import { EventEmitter } from "node:events";

export class Washer extends EventEmitter {
  constructor(number) {
    super();
    this.number = number;
  }

  start(name, ms) {
    console.log(`Washer ${this.number} started for ${name}`);
    setTimeout(() => {
      this.emit("done", name, this.number);
    }, ms);
  }
}

The super() call sets up the emitter part of the washer, so it has somewhere to keep its listeners. Then the constructor stores the machine’s number. The .start() method takes the housemate’s name and a cycle time in milliseconds, which is a toy stand-in for an hour-long wash.

The timer’s callback is an arrow function, which takes its this from the code around it, as this binding explains. So inside the callback, this is the same washer that .start() was called on. When the timer is due, the washer calls this.emit("done", name, this.number) on itself.

Why send two values with the event? Everything you pass to .emit() after the event’s name reaches each listener as its arguments, in the same order. A listener that gets the name and the machine number can write a useful message without knowing which washer it was added to. Here’s laundry.mjs, which uses one listener for two washers:

Language: JavaScript Filename: laundry.mjs
import { Washer } from "./washer.mjs";

const washer1 = new Washer(1);
const washer2 = new Washer(2);

const tellOwner = (name, number) => {
  console.log(`${name}, collect your laundry from washer ${number}`);
};
washer1.on("done", tellOwner);
washer2.on("done", tellOwner);

washer1.start("Ines", 300);
washer2.start("Kofi", 200);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.