Function: definePlayer()
API / @xmachines/play-xstate / definePlayer
function definePlayer<TMachine>(config): PlayerFactory<TMachine>;Defined in: packages/play-xstate/src/define-player.ts:61
Create a player factory from an XState machine
Factory pattern that accepts an XState v5 machine, returning a function that creates PlayerActor instances. This enables creating multiple actor instances from a single configuration, useful for testing, multi-instance scenarios, or server-side rendering.
Type Parameters
| Type Parameter | Description |
|---|---|
TMachine extends AnyStateMachine | XState v5 state machine type |
Parameters
| Parameter | Type | Description |
|---|---|---|
config | PlayerConfig<TMachine> | Player configuration object |
Returns
PlayerFactory<TMachine>
Factory function that creates actor instances with optional input context
Examples
Basic player factory
import { setup } from "xstate";import { definePlayer } from "@xmachines/play-xstate";
const machine = setup({}).createMachine({ initial: "idle", states: { idle: { meta: { route: "/" } }, active: { meta: { route: "/active" } }, },});
const createPlayer = definePlayer({ machine });const actor = createPlayer();actor.start();Multiple actor instances from single factory
const createPlayer = definePlayer({ machine });
// Create actors for different usersconst alice = createPlayer({ userId: "alice" });const bob = createPlayer({ userId: "bob" });
alice.start();bob.start();
// Independent state machinesconsole.log(alice.state.get() !== bob.state.get());See
- Play RFC
- PlayerActor for the concrete actor implementation
- PlayerConfig for configuration options
- PlayerFactory for factory function signature