2.0.1 • Published 8 months ago

@uictoria1/fsm v2.0.1

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
8 months ago

Minimalistic Finite State Machine

Getting started

import { createMachine, type StateObject } from "fsm";

const states: StateObject[] = [
  {
    state: "idle",
    on: {
      start: "starting",
    },
  },
  // multiple states
  {
    state: "starting",
    on: {
      starting_done: "started",
      start_error: "error",
    },
  },
  {
    state: "started",
    on: {
      stop: "stopping",
    },
  },
  {
    state: "stopping",
    on: {
      stopping_done: "idle",
    },
  },
  {
    state: "error",
    on: {
      restart: "idle",
    },
  },
];

const fsm = createMachine(states);
console.log("currentState", fsm.current); // idle

fsm.dispatch("start");
console.log("currentState", fsm.current); // starting

fsm.dispatch("starting_done");
console.log("currentState", fsm.current); // started

// reset to any state or initial state by default
fsm.reset("idle");
console.log("currentState", fsm.current); // idle
2.0.1

8 months ago

2.0.0

8 months ago

1.0.0

2 years ago