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 insrc/game/eventEmitter.ts.
Constraint
TEmitterEvent must extend EmitterEventBase:
Return value
Methods
subscribeOnMount()
Registers a map of handlers inside a Svelte component’sonMount lifecycle. Automatically unsubscribes when the component is destroyed.
EmitterEventHandlerMap type is keyed by every type in the TEmitterEvent union, and each value receives the narrowed event type for that key:
broadcast()
Synchronously dispatches an event to all current subscribers. Use this for fire-and-forget updates (show/hide a component, update a counter).Set and calls each handler synchronously:
broadcastAsync()
Dispatches an event to all subscribers and returnsPromise.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).
