fn
createMachine
testedCreate a type-safe synchronous finite state machine with context
Example
ts
const machine = createMachine({
initial: 'idle',
context: { retries: 0 },
states: {
idle: {
on: { START: 'running' },
},
running: {
on: {
FAIL: {
target: 'idle',
guard: (ctx) => ctx.retries < 3,
action: (ctx) => { ctx.retries++; },
},
STOP: 'idle',
},
},
},
});
machine.send('START'); // 'running'Signatures
ts
export function createMachine<
const States extends Record<string, SyncStateNodeConfig<Context>>,
Context,
>(config: {
initial: NoInfer<ExtractStates<States>>;
context: Context;
states: States;
}): StateMachine<ExtractStates<States>, ExtractEvents<States>, Context>;ts
export function createMachine<
const States extends Record<string, SyncStateNodeConfig<undefined>>,
>(config: {
initial: NoInfer<ExtractStates<States>>;
states: States;
}): StateMachine<ExtractStates<States>, ExtractEvents<States>, undefined>;Parameters
| Parameter | Type | Description |
|---|---|---|
config | { initial: string; context?: unknown; states: Record<string, SyncStateNodeConfig<any>>; } | — |
Returns
StateMachine<string, string, undefined>