All Challenges

Custom Event Emitter & Pub/Sub Widget Bus

TypeScriptMediumSalesforceStripeShopify
30:00

Problem Brief

Implement an in-memory Pub/Sub EventEmitter (`subscribe`, `unsubscribe`, `publish`) powering decoupled UI subscriber panels.

Requirements

  • Expose `window.bus` with `subscribe(topic, cb)` returning an unsubscribe cleanup function.
  • Clicking `[data-testid="pub-btn"]` publishes `"Order Placed"` to active subscribers.
  • Clicking `[data-testid="unsub-a"]` detaches Subscriber A cleanly without affecting Subscriber B.
Progressive Hints (1)
  • Store `Map<string, Set<Function>>`; return `() => listeners.delete(cb)` from `subscribe`.
Reference Solution

Stuck? Load the verified reference code into your editor.

Automated Test Suite

1 Test Cases Ready
  • Notifies all subscribers and respects unsubscribe cleanupPublish reaches A & B; after unsub-a, Publish increments only B
solution.html
Live Sandboxed Previewiframe sandbox
Starter template loaded — add your <script> logic in the editor (or open Reference Solution) to make the widget interactive.