Skip to content

Function: toAtomState()

API / @xmachines/play-actor / toAtomState

function toAtomState(state): Record<string, unknown>;

Defined in: packages/play-actor/src/provider-guards.ts:36

Safely coerce a spec’s state field to a plain object for createAtom.

spec.state is typed as unknown in PlaySpec. At runtime it can be null, undefined, a primitive, or a plain object depending on what the machine author put in the view spec. createAtom requires a plain object as its initial value — anything else produces a broken store at runtime.

Only plain objects (prototype is Object.prototype or null) are accepted and returned as-is. Everything else — null, undefined, primitives, arrays, class instances, and built-in objects (Date, Map, Set, etc.) — falls back to a fresh {}, preventing silent broken-store bugs at runtime.

Parameters

ParameterTypeDescription
stateunknownThe raw spec.state value from a PlaySpec.

Returns

Record<string, unknown>

state itself when it is a plain object, otherwise a new empty object.