createEventEmitter creates a typed pub/sub channel. Game logic calls broadcast or broadcastAsync to fire events; Svelte components subscribe via subscribeOnMount and automatically unsubscribe when they are destroyed.
Function signature
EmitterEventBase
Every event type must extendEmitterEventBase:
type field is the discriminant used to route events to handlers.
Parameters
createEventEmitter takes no parameters. The event type is supplied as a TypeScript generic:
Return value
object
An object with three methods.
eventEmitter.broadcast
broadcast for fire-and-forget notifications where you do not need to wait for the subscriber to finish, such as showing or hiding UI elements and updating reactive state.
eventEmitter.broadcastAsync
Promise.all over all subscriber return values. Use broadcastAsync when you need to await the completion of subscriber work before continuing — for example, waiting for an animation to finish, a countdown to complete, or a transition to resolve.
eventEmitter.subscribeOnMount
onMount, which means:
- Subscription is set up when the component mounts.
- The unsubscribe function returned by the internal
subscribeHandlerMapis called automatically byonMount’s cleanup when the component is destroyed. - You do not need to manage the subscription lifecycle manually.
subscribeOnMount must be called at the top level of a Svelte component’s <script> block, not inside a callback or conditional. This is required because Svelte’s onMount must be called synchronously during component initialisation.Context helpers
These functions fromutils-event-emitter/context store and retrieve an eventEmitter instance in Svelte’s component context, making it available to child components without prop-drilling.
setContextEventEmitter
@@eventEmitter. Call this in a parent component (typically the root game component) to make the emitter available to its entire subtree.
getContextEventEmitter
TEmitterEvent type to get full type safety.
Usage example
Using context
Real-world pattern (from the Lines game)
The Lines game creates a single emitter that is shared across all game logic and UI components:EmitterEvent* type, keeping event definitions co-located with the components that handle them.