Find the bug · #43 · Races Easy JavaScript
One order, four notifications
The more pages you visit, the more “order placed” toasts you get for a single order.
On this page
The report
An “order placed” toast is shown on the orders page. Users who have moved around the app a bit get the same toast three or four times for one order; the more pages they visit, the more toasts.
The code
Read it first: what should it do? Then click the line where you think the bug is.
// The app's event bus.
class Bus {
constructor() { this.handlers = []; }
on(fn) { this.handlers.push(fn); }
off(fn) { this.handlers = this.handlers.filter((h) => h !== fn); }
emit(data) { this.handlers.forEach((h) => h(data)); }
}
// The "order placed" toast. The page mounts it on arrival and unmounts it on leaving.
class OrderToast {
constructor(bus, toast) {
this.bus = bus;
this.toast = toast;
}
mount() {
this.bus.on((order) => this.toast('Order ' + order.id + ' placed'));
}
unmount() {
this.bus.off((order) => this.toast('Order ' + order.id + ' placed'));
}
}Read it, then pick a line.
off removes a handler that is the same function. Is it?
What it prints
Running it on a small example prints the output on the left. It should print the output on the right.
const bus = new Bus();
const shown = [];
const widget = new OrderToast(bus, (msg) => shown.push(msg));
for (let visit = 0; visit < 3; visit++) {
widget.mount();
widget.unmount();
}
widget.mount();
bus.emit({ id: 42 });
console.log(shown.length + ' toast(s)');It prints
4 toast(s)
It should print
1 toast(s)
Show the answer: the bug, the fix, and how you’d find it
The bug (lines 16, 19)
Lines 16 and 19 each create a brand-new arrow function. off removes handlers that are the same function (h !== fn), and the one passed on line 19 has never been registered, so nothing is removed. Every visit leaves one more handler behind. Create the handler once and pass that same function to both on and off.
The fix
11 constructor(bus, toast) {12 this.bus = bus;13 this.toast = toast;14 this.onOrder = (order) => this.toast('Order ' + order.id + ' placed');14 }15 mount() {16 this.bus.on((order) => this.toast('Order ' + order.id + ' placed'));17 this.bus.on(this.onOrder);17 }18 unmount() {19 this.bus.off((order) => this.toast('Order ' + order.id + ' placed'));20 this.bus.off(this.onOrder);20 }21}How you’d find it
- Reproduce: mount and unmount a few times, then emit one order. One toast per visit.
- Count the handlers after an unmount:
bus.handlers.lengthstill grows. Sooffisn’t removing anything. offcompares functions by identity. Two arrow functions with the same text are still two different functions.
The test that catches it
It fails on the code above and passes once it’s fixed. Keep it, so the bug can’t come back.
const bus = new Bus();
const shown = [];
const w = new OrderToast(bus, (m) => shown.push(m));
w.mount();
w.unmount();
w.mount();
bus.emit({ id: 7 });
assert.deepEqual(shown, ['Order 7 placed']);
w.unmount();
bus.emit({ id: 8 });
assert.equal(shown.length, 1);Where else this bites
element.removeEventListener("click", () => …)never removes anything.- A React
useEffectthat subscribes without returning a cleanup.