Multiplexing callbacks
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Callback multiplexing means taking one event source and safely dispatching it to many listeners. It is useful in UI event systems, background task completion hooks, and internal service event buses. The challenge is preserving predictable behavior when listeners fail, unsubscribe, or run asynchronously.
What Multiplexing Should Guarantee
A good callback multiplexer defines clear rules:
- Registration and unregistration are explicit.
- Listener execution order is deterministic or documented.
- One failing listener does not break all others.
- One-time listeners are supported for lifecycle events.
Without these rules, callback systems become fragile and difficult to debug.
Build a Minimal Multiplexer
Start with a small event bus abstraction.
This baseline covers fan-out and unsubscribe mechanics.
Add Failure Isolation
One listener throwing an exception should not prevent others from running.
If failure should abort dispatch, make that a documented policy instead of accidental behavior.
Support One-Time Listeners
Lifecycle events often need listeners that run once and auto-remove.
This avoids manual unsubscribe boilerplate and reduces leak risk.
Async Listener Handling
If listeners can return promises, decide whether dispatch should wait for all listeners or fire-and-forget.
Await-all pattern:
This model is useful when downstream state should not progress until all listeners finish.
Prevent Listener Leaks
Multiplexers are leak-prone when listeners are never removed. Use unsubscribe handles and lifecycle hooks to clean up.
Guidelines:
- Return unsubscribe function from
on. - Use
oncefor transient listeners. - Clear listeners on component teardown.
- Track listener counts for diagnostics in long-lived processes.
A small leak in callback systems can cause duplicate side effects and memory growth over time.
Testing Multiplexing Behavior
Important tests:
- Multiple listeners receive same payload.
- Unsubscribe prevents future callbacks.
- One-time listener runs once.
- One listener failure does not block others.
- Async mode resolves all listeners before completion.
These tests lock behavior and make refactoring safe.
Common Pitfalls
- Not returning unsubscribe handles from registration.
- Letting one callback exception abort the full fan-out path unintentionally.
- Mixing sync and async listeners without a defined dispatch policy.
- Mutating listener arrays while iterating without defensive handling.
- Forgetting cleanup in long-lived modules and creating callback leaks.
Summary
- Callback multiplexing needs explicit delivery, error, and lifecycle policies.
- Start with a small bus that supports
on,off, andemit. - Add failure isolation so one bad listener does not break all listeners.
- Support
onceand async dispatch modes based on use case. - Treat unsubscribe discipline as essential for correctness and memory safety.

