Skip to content

dldc-packages/stachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stachine

A TypeScript state machine with events and effects

npm install @dldc/stachine

Gist

import { createStachine } from "@dldc/stachine";

type State = { state: "Home" } | { state: "Bed" } | { state: "Work" } | {
  state: "Error";
};
type Action = { action: "Commute" } | { action: "Wake" } | { action: "Sleep" };

const machine = createStachine<State, Action>({
  initialState: { state: "Home" },
  createErrorState: () => ({ state: "Error" }),
  states: {
    Home: {
      actions: {
        Commute: () => ({ state: "Work" }),
        Sleep: () => ({ state: "Bed" }),
      },
    },
    Work: {
      actions: {
        Commute: () => ({ state: "Home" }),
      },
    },
    Bed: {
      actions: {
        Wake: () => ({ state: "Home" }),
      },
    },
    Error: {},
  },
});

About

A TypeScript state machine with events and effects

Resources

License

Stars

Watchers

Forks

Packages

No packages published