silk.machine.xstate.

A Definition written as an xstate machine config, the JSON that xstate v6 reads with machineFromJSON and that Stately Studio draws.

Details#

Every on, always and after entry is written as an array of transitions, which xstate accepts for one transition too. A guard is written as an object whose type is the name Guards.named gave it. A guard without a name gets the name <state>.<event>, <state>.always for an eventless one. A tool shows the name, and the function stays in the Silk source. The effects a row leaves are its actions, each an object whose type is the effect as text. The reason of a Chart.refuse row is the description of its transition. A Chart.onDone row is a transition on the event xstate.done.state that matches the id of the state, as xstate v6 writes it.

Xstate.read takes the same format back. It accepts a transition as a state name, as an object with target and an optional guard, or as an array of those, and a guard as a name or an object with a type, which are the forms xstate v5, xstate v6 and Stately Studio write. A done transition is read from xstate.done.state or from a v5 onDone. A transition on an event without a target takes the event and changes no state, which Chart.stay does, and the event * is the transition that any event takes. The name of a guard and the type of each action are kept. Context and other keys a Definition has no place for are left out.

A state with a history and child states is written with one more child, the history state hist, whose target is the child state to enter when there is no history yet, and a transition to the history targets <state>.hist. Reading takes any history child state as the history of its parent, kept in the context field last<State>, and a target with a dot as a transition to the history of the state before the dot.

Errors#

XstateError from Xstate.read when the config has no initial state, has a part of the wrong JSON kind, or names a delay that is not a number of milliseconds.

Gotchas#

xstate names the child states of a compound state but not the region that holds them. A compound state Red is read as one region with the field region and the chart name RedRegion. Each child state of a parallel state is read as a region whose field and chart name are the child's key. Rename them in the definition before generating Silk source.

Import as Xstate with import silk.machine.xstate { Xstate }.

Public declarations: 2.

XstateError#

silk
pub union XstateError

Why an xstate config could not be read as a Definition.

NotAnObject#

silk
NotAnObject

The config or one of its states is not a JSON object.

MissingInitial#

silk
MissingInitial

The config or a compound state has no initial state name.

BadTransition#

silk
BadTransition

A transition is not a state name, an object or an array of those.

BadDelay#

silk
BadDelay

A key of after is not a number of milliseconds.

Implementation Debug for XstateError#

silk
impl Debug for XstateError

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

Writes the name of the variant.

Xstate#

silk
pub struct Xstate

Implementation Xstate#

silk
impl Xstate

Associated function read#

silk
pub effect fn read(bytes: &[u8]) -> Definition
    ! JsonError | OutOfMemoryError | XstateError
    ? &mut Allocator

Reads an xstate machine config in JSON as a Definition.

Associated function write#

silk
pub effect fn write(definition: &Definition) -> ()
    ! WriterError
    ? &mut Writer

Writes definition as an xstate machine config in JSON. A state with one region is written as a compound state and a state with several as a parallel state.

Category: State machines.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page