update readme
This commit is contained in:
parent
0123a2e9b4
commit
48384bdbdf
36
README.md
36
README.md
@ -4,33 +4,29 @@ A JavaScript library for creating non-deterministic finite state machines with t
|
||||
|
||||
## Basic Usage
|
||||
|
||||
Obligatory traffic light example:
|
||||
|
||||
```js
|
||||
import { createMachine } from "fsm";
|
||||
|
||||
const light = createMachine({
|
||||
data: { brightness: 0 },
|
||||
states: {
|
||||
off: 1, // initially active
|
||||
on: 0,
|
||||
dimming: 0
|
||||
},
|
||||
const trafficLight = createMachine({
|
||||
states: { green: 1, yellow: 0, red: 0 },
|
||||
transitions: [
|
||||
{
|
||||
from: "off",
|
||||
to: "on",
|
||||
when: ctx => ctx.get("brightness") > 0
|
||||
},
|
||||
{
|
||||
from: "on",
|
||||
to: "dimming",
|
||||
when: ctx => ctx.get("brightness") < 50
|
||||
}
|
||||
{ from: "green", to: "yellow" },
|
||||
{ from: "yellow", to: "red" },
|
||||
{ from: "red", to: "green" }
|
||||
]
|
||||
});
|
||||
|
||||
light.data.brightness = 75;
|
||||
light.step(); // evaluates transitions
|
||||
console.log(light.has("on")); // true
|
||||
// cycle through lights
|
||||
trafficLight.step();
|
||||
console.log(trafficLight.state); // ['yellow']
|
||||
|
||||
trafficLight.step();
|
||||
console.log(trafficLight.state); // ['red']
|
||||
|
||||
trafficLight.step();
|
||||
console.log(trafficLight.state); // ['green']
|
||||
```
|
||||
|
||||
## State Ticking
|
||||
|
||||
Loading…
Reference in New Issue
Block a user