javascript 32 lines · 8 steps

Event delegation with a clean teardown

A single listener on a root element handles events for many matching descendants, with an unsubscribe function for cleanup.

Explained by highlit
1function delegate(root, eventType, selector, handler) {
2 const listener = (event) => {
3 let node = event.target;
4 while (node && node !== root) {
5 if (node.matches(selector)) {
6 handler.call(node, event, node);
7 return;
8 }
9 node = node.parentNode;
10 }
11 };
12 root.addEventListener(eventType, listener);
13 return () => root.removeEventListener(eventType, listener);
14}
15 
16class EventDelegator {
17 constructor(root) {
18 this.root = root;
19 this.cleanups = [];
20 }
21 
22 on(eventType, selector, handler) {
23 const off = delegate(this.root, eventType, selector, handler);
24 this.cleanups.push(off);
25 return this;
26 }
27 
28 destroy() {
29 this.cleanups.forEach((off) => off());
30 this.cleanups = [];
31 }
32}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1One listener on a shared ancestor can serve many children, so you avoid attaching (and leaking) handlers on every element.
  2. 2Returning an unsubscribe closure makes teardown symmetric with setup and keeps the listener reference in scope.
  3. 3Collecting cleanup functions lets a coordinator dispose of all its listeners in one call.

Related explainers

javascript
const ROLE_PERMISSIONS = {
  admin: ['users:read', 'users:write', 'billing:read', 'billing:write'],
  manager: ['users:read', 'billing:read'],
  member: ['users:read'],

Role-based permissions middleware in Express

authorization middleware rbac
Intermediate 9 steps
javascript
function attachThousandSeparators(input, { locale = 'en-US' } = {}) {
  const formatter = new Intl.NumberFormat(locale);
  const groupSep = formatter.format(11111).replace(/\d/g, '')[0] || ',';
  const decimalSep = formatter.format(1.1).replace(/\d/g, '')[0] || '.';

Live thousand separators without losing the caret

dom intl caret-preservation
Advanced 8 steps
javascript
import { useReducer, useEffect } from "react";
 
const initialState = { status: "idle", data: null, error: null };
 

Building a data-fetching hook in React

custom-hooks usereducer data-fetching
Intermediate 9 steps
javascript
const express = require('express');
const app = express();
 
app.get('/health', (req, res) => res.json({ status: 'ok' }));

Graceful shutdown in an Express server

graceful-shutdown signal-handling connection-tracking
Advanced 9 steps
javascript
import { useState, useEffect, useCallback } from 'react';
 
function getColumnCount(width) {
  if (width < 640) return 1;

A responsive column hook in React

custom-hooks debouncing responsive-design
Intermediate 7 steps
javascript
function initCharacterCounter(textarea, options = {}) {
  const maxLength = options.maxLength ?? 280;
  const warnThreshold = options.warnThreshold ?? 0.9;
 

A live character counter for textareas

dom closures accessibility
Intermediate 7 steps

Share this explainer

Here's the card — post it anywhere.

Event delegation with a clean teardown — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code