AniUI Academy

Replay event delegation up an ancestor chain

hardDOM & Utilities+350 XPPractice

One listener on a container beats a hundred listeners on rows, which is why delegation is everywhere. What it costs you is intuition about ordering: which handler runs first when two match, and what stopPropagation actually stops. This models it exactly.

You are given chain, an array of plain nodes ordered from the event target first to the outermost ancestor last — the same order as event.composedPath(). Each node is { tag, attrs }, and attrs may be missing. You are also given handlers, an array of { name, match, stopPropagation } in registration order, where stopPropagation is optional.

A match is a plain object that may contain tag, id and class. A node matches when every field present in match matches: tag compared case-insensitively against node.tag; id compared exactly against attrs.id; class required as a whole whitespace-separated token of attrs.class. An empty match object matches every node.

Implement dispatch(chain, handlers) returning an array of handler name values in firing order. Walk chain from index 0 outwards, and at each node fire every matching handler in registration order, appending its name. If any handler that fired at the current node has stopPropagation: true, all remaining matching handlers on that same node still fire, and then the walk stops without visiting any further ancestors. Return [] when nothing matches.

Arguments

chainArray<{ tag: string; attrs?: object }>
The ancestor path from the event target outward, in the same order as event.composedPath().
handlersArray<{ name: string; match: object; stopPropagation?: boolean }>
Delegated handlers in registration order. match can contain tag, id and/or class; an empty object matches every node.

Returns

string[] — The handler names in firing order — node by node from the target outwards, in registration order within each node.

Examples

const chain = [
  { tag: "span", attrs: { class: "icon" } },
  { tag: "button", attrs: { class: "btn primary" } },
  { tag: "div", attrs: { id: "toolbar" } }
];
dispatch(chain, [
  { name: "toolbar", match: { id: "toolbar" } },
  { name: "button", match: { tag: "BUTTON" } },
  { name: "icon", match: { class: "icon" } }
]);
// => ["icon", "button", "toolbar"]

Handlers fire from the target outwards, matching one node at a time.

const chain = [
  { tag: "span", attrs: { class: "icon" } },
  { tag: "button", attrs: { class: "btn" } },
  { tag: "div", attrs: { id: "toolbar" } }
];
dispatch(chain, [
  { name: "icon", match: { class: "icon" } },
  { name: "btn", match: { class: "btn" }, stopPropagation: true },
  { name: "btn-late", match: { tag: "button" } },
  { name: "toolbar", match: { id: "toolbar" } }
]);
// => ["icon", "btn", "btn-late"]

stopPropagation lets the rest of the handlers on that node fire, then halts the ancestor walk.

dispatch([{ tag: "button", attrs: { class: "btn primary" } }], [
  { name: "partial", match: { class: "prim" } },
  { name: "any", match: {} }
]);
// => ["any"]

class matches whole tokens only, and {} is a universal match.

What it has to do

  • Return handler names in firing order: node by node from the target outwards, and by registration order within a node.
  • Match on tag case-insensitively, id exactly, and class as a whole whitespace-separated token.
  • Treat an empty match object as matching every node.
  • Let every matching handler on the current node fire even when one of them sets stopPropagation, then stop before the next ancestor.
  • Return [] when no handler matches, and tolerate nodes with no attrs.

Notes

  • This mirrors the DOM: event.stopPropagation() lets same-level listeners finish but prevents the event climbing to ancestors. event.stopImmediatePropagation() would also cut off the remaining handlers on this node — a different problem.
  • Record the stop as a flag inside the inner loop and only act on it afterwards; breaking out immediately would skip handlers registered later on the same node, which is wrong.
Asked atGoogleMicrosoft

Stuck? The javascript course covers everything this challenge needs.

Try it yourself
Loading playground...
5 tests · +350 XP

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.