update readme

This commit is contained in:
rts-pyers 2025-08-11 21:20:01 -04:00
parent 0123a2e9b4
commit 48384bdbdf

View File

@ -4,33 +4,29 @@ A JavaScript library for creating non-deterministic finite state machines with t
## Basic Usage ## Basic Usage
Obligatory traffic light example:
```js ```js
import { createMachine } from "fsm"; import { createMachine } from "fsm";
const light = createMachine({ const trafficLight = createMachine({
data: { brightness: 0 }, states: { green: 1, yellow: 0, red: 0 },
states: {
off: 1, // initially active
on: 0,
dimming: 0
},
transitions: [ transitions: [
{ { from: "green", to: "yellow" },
from: "off", { from: "yellow", to: "red" },
to: "on", { from: "red", to: "green" }
when: ctx => ctx.get("brightness") > 0
},
{
from: "on",
to: "dimming",
when: ctx => ctx.get("brightness") < 50
}
] ]
}); });
light.data.brightness = 75; // cycle through lights
light.step(); // evaluates transitions trafficLight.step();
console.log(light.has("on")); // true console.log(trafficLight.state); // ['yellow']
trafficLight.step();
console.log(trafficLight.state); // ['red']
trafficLight.step();
console.log(trafficLight.state); // ['green']
``` ```
## State Ticking ## State Ticking