pure
pure<
E
>(machine
:StateMachine
<E
>):PureStateMachine
<E
>
Defined in: state-machine-pure.ts:38
Creates a minimal, pure state machine interface exposing only getState
and send
.
Useful for consumers who only need to read state and dispatch events, without lifecycle hooks.
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
E extends TransitionEvent <unknown , unknown > |
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
machine | StateMachine <E > | The full StateMachine instance. |
Returns
Section titled “Returns”PureStateMachine
<E
>
A PureStateMachine exposing only getState
and send
.
Source
Section titled “Source”This function is useful for creating a lightweight wrapper around a state machine, allowing consumers to interact with the state machine without needing access to its full lifecycle methods. It provides a simplified interface that focuses on state retrieval and event dispatching.
export function pure<E extends TransitionEvent>( machine: StateMachine<E>): PureStateMachine<E> { const { getState, send } = machine; return { getState, send, };}
Example
Section titled “Example”import { matchina } from "matchina";
// Create a fancy machine with internal APIconst machine = matchina({ states: { Idle: undefined, Active: (user: string) => ({ user }), }, transitions: { Idle: { activate: "Active" }, Active: { deactivate: "Idle" }, }, setup: (machine) => { // Internal API, hooks, etc. machine.log = () => console.log(machine.getState()); },});
// Expose only the pure facade for external consumersexport const publicMachine = pure(machine);// publicMachine exposes only getState and send