Skip to main content
utils-event-emitter implements event-driven programming for the SDK. It connects the JavaScript scope (book event handlers, state machines) to Svelte component scope without passing props through deep component trees.

createEventEmitter()

Factory function typed by a union of all possible emitter events. Create it once per game in src/game/eventEmitter.ts.

Constraint

TEmitterEvent must extend EmitterEventBase:
In practice every game defines a discriminated union type:

Return value

Methods

subscribeOnMount()

Registers a map of handlers inside a Svelte component’s onMount lifecycle. Automatically unsubscribes when the component is destroyed.
The EmitterEventHandlerMap type is keyed by every type in the TEmitterEvent union, and each value receives the narrowed event type for that key:
Example — sync handler:

broadcast()

Synchronously dispatches an event to all current subscribers. Use this for fire-and-forget updates (show/hide a component, update a counter).
Internally it iterates the subscription Set and calls each handler synchronously:

broadcastAsync()

Dispatches an event to all subscribers and returns Promise.all of every handler’s return value. Use await eventEmitter.broadcastAsync(...) when you need to wait for all animations or async operations triggered by the event to finish.

ContextEventEmitter

utils-event-emitter also exports context helpers so eventEmitter can be read by any descendant component without prop drilling:
setContextEventEmitter must be called at the entry level of the app (same place as the other setContext* calls).

Typical setup pattern