Skip to main content
Module

x/xstate/src/createMachine.ts>createMachine

Actor-based state management & orchestration for complex app logic.
Go to Latest
function createMachine
import { createMachine } from "https://deno.land/x/xstate@xstate@5.9.1/src/createMachine.ts";

Creates a state machine (statechart) with the given configuration.

The state machine represents the pure logic of a state machine actor.

Examples

Example 1

  import { createMachine } from 'xstate';

  const lightMachine = createMachine({
    id: 'light',
    initial: 'green',
    states: {
      green: {
        on: {
          TIMER: { target: 'yellow' }
        }
      },
      yellow: {
        on: {
          TIMER: { target: 'red' }
        }
      },
      red: {
        on: {
          TIMER: { target: 'green' }
        }
      }
    }
  });

  const lightActor = createActor(lightMachine);
  lightActor.start();

  lightActor.send({ type: 'TIMER' });

Type Parameters

TContext extends MachineContext
TEvent extends AnyEventObject
TActor extends ProvidedActor
TAction extends ParameterizedObject
TGuard extends ParameterizedObject
TDelay extends string
TTag extends string
TInput
TOutput extends NonReducibleUnknown
TEmitted extends EventObject
optional
TTypesMeta extends TypegenConstraint = TypegenDisabled

Parameters

The state machine configuration.

DEPRECATED: use setup({ ... }) or machine.provide({ ... }) to provide machine implementations instead.

Returns

StateMachine<TContext, TEvent, Cast<ToChildren<TActor>, Record<string, AnyActorRef | undefined>>, TActor, TAction, TGuard, TDelay, "matchesStates" extends keyof TTypesMeta ? ToStateValue<Cast<TTypesMeta["matchesStates"], TestValue>> : StateValue, Prop<ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag, TEmitted>["resolved"], "tags"> & string, TInput, TOutput, TEmitted, ResolveTypegenMeta<TTypesMeta, TEvent, TActor, TAction, TGuard, TDelay, TTag, TEmitted>>