diff --git a/README.md b/README.md index e7699ec..0c3e914 100644 --- a/README.md +++ b/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