Skip to main content
utils-xstate wraps XState v5 to provide a robust finite state machine for the betting lifecycle. It eliminates deeply nested if-else logic and provides a clean API for UI components to react to game state.

createXstate()

Creates the reactive stateXstate and stateXstateDerived objects. Call this once per game.

stateXstate

stateXstateDerived

A collection of helper functions that each call matchesState from XState internally:
isPlaying() is the most commonly used helper — it returns true whenever any bet flow is active.

State machine states

The gameActor machine has five states:

createGameActor()

Builds the XState machine and actor using setup() + createActor() from XState:

createIntermediateMachines()

Creates the three sub-machines (bet, autoBet, resumeBet) that are invoked by the game actor. Each is built from primary machine actors (newGame, playGame, endGame, resumeGame) provided by your game.

createPrimaryMachines()

Creates the four XState fromPromise actors that form the inner bet loop. You provide callbacks that connect the machine to your game’s RGS requests and animation layer.

BET_TYPE_METHODS_MAP

Internally, createPrimaryMachines classifies each bet into one of three types and calls end-round at the appropriate time:
The timing of end-round differs by bet type. Whether a bet can be resumed from the authenticate request depends on when end-round is called. bonusWin bets can be resumed because end-round is deferred until after animations.

ContextXstate

Context key: '@@xstate'.

UI usage example

The most common usage is disabling buttons while the game is playing:
Because stateXstateDerived functions are backed by Svelte $state, any component that reads them is automatically reactive — the button re-enables as soon as the machine transitions back to idle.