micro-fsm

A tiny finite state machine library with rich TypeScript support. Only 222 bytes.

Usage no npm install needed!

<script type="module">
  import microFsm from 'https://cdn.skypack.dev/micro-fsm';
</script>

README

Micro FSM

A tiny finite state machine library with rich TypeScript support. Only 222 bytes.

TypeScript

This library takes advantage of TypeScript's extensive inference capabilities.

Both the available states and events are inferred from the setup.

Simple example

import fsm from "fluentstatemachine";

const machine = fsm("green") // initial state
  .event("warn",  { from: ["green"],           to: "yellow" })
  .event("panic", { from: ["green", "yellow"], to: "red" })
  .event("calm",  { from: ["red"],             to: "yellow" })
  .event("clear", {                            to: "green" })
  .event("work",  { from: ["green"] })
  .build();

// machine.current === "green"

machine.panic();        // machine.current === "red"
                        // machine.prev === "green"

machine.work();         // Throws "Can't work from red"

if (machine.calm.can()) // true - Can calm from 'green'
  machine.calm();       // machine.current === "yellow"