Jump to a problem, path or page
Jump to a problem, path or page
01 · Develop the design
Use a token-indexed subscription registry and a dispatcher that iterates a stable snapshot in registration order. Isolate listener failures and define synchronous, reentrant and optional asynchronous behavior explicitly.
Stage 1
Pick dispatch semantics first
For this exercise, use synchronous registration-order dispatch to a snapshot. New subscriptions take effect on the next publication; removed snapshot listeners still receive the current event.
Stage 2
Keep tokens separate from callbacks
The same callback may subscribe more than once. A unique token identifies one subscription and lets unsubscribe target it exactly.
Stage 3
Bound reentrancy and async work
Nested publish calls need an explicit recursion/depth policy. An asynchronous adapter needs a bounded queue and an overload result rather than unlimited promises.
02 · Work through the method
03 · Justify the choices
Which subscriptions receive a publication?
A stable snapshot captured at the start of publish
Snapshot semantics make subscription changes during dispatch predictable.
What if one listener throws?
Collect/report the error and continue according to the documented policy
Error isolation prevents one subscriber from accidentally blocking unrelated subscribers.
What should unsubscribe(token) do twice?
Succeed harmlessly without affecting another subscription
Idempotent unsubscribe simplifies cleanup and preserves subscription identity.
04 · Plan the evidence