packages/*.
Why Svelte Context?
Some shared state requires typed inputs to create (e.g. theEmitterEvent union type, or the assets configuration). This makes plain global variables impractical. Svelte context lets the app provide its concrete typed instances, which packages then consume through typed getContext helpers — without either side needing to know about the other’s internals.
Context is hierarchical: only descendant components can access a context set by a parent. This is why
setContext() is called at the entry level of the app.Setting Context at the Entry Level
Inapps/lines, context is set once in context.ts and called from the entry component:
The Four Contexts
ContextEventEmitter
Provides the typedeventEmitter instance. The generic type parameter pins the emitter to the game’s concrete EmitterEvent union, giving full TypeScript intellisense for broadcast, broadcastAsync, and subscribeOnMount.
ContextXstate
Provides the XState state and its derived helpers. Created bycreateXstate() from utils-xstate.
See Game State Machine for full documentation.
ContextLayout
Provides canvas size and layout information. Created bycreateLayout() from utils-layout. Because PIXI.Application is configured with resizeTo: window, the canvas is always the size of the browser window — this context exposes reactive derivations of that.
canvasSizes provides the boundaries every component needs to place itself correctly.
layoutType breakpoints are derived from the canvas aspect ratio and size:
isStacked returns true for portrait and almostSquare layouts, which is useful for stacking UI elements vertically.
ContextApp
Provides the PixiJS application state. Created bycreateApp() from pixi-svelte.
How Packages Consume Context
Packages never callsetContext — they only call getContext. For example, components-layout uses getContextLayout() from utils-layout:
<App /> from pixi-svelte — which is always an ancestor of layout components — was rendered inside a component tree where setContextLayout() was already called.
The same pattern applies for all four contexts: the app sets them once at the root; any descendant (whether in the app or in a package) reads them with the corresponding getContext*() helper.