Event Emitter

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Event Emitter

Approach

Maintain a dictionary mapping event names to arrays of callback functions. The on method pushes a callback and returns an unsubscribe function that filters it out. The emit method iterates over all callbacks for the given event and invokes each with the provided data.

Complexity Analysis

Time
O(n) for emit where n is subscriber count
Space
O(n) for stored callbacks

Pattern

Event Emitter

Why It Works

The publish-subscribe pattern decouples event producers from consumers through a shared registry, and returning an unsubscribe closure from on() provides clean lifecycle management.

Updated Feb 2026