wstate
v2.1.0
Published
A finite state machine library
Downloads
2
Maintainers
Readme
Welcome to wstate 👋
A library for finite state machines.
Installation
In a browser:
<script src="state-machine.js"></script>
after downloading the source or the minified version
Using npm:
npm install --save-dev wstate
In Node.js:
var StateMachine = require("wstate");
Usage
A state machine can be constructed using:
var fsm = new StateMachine({
init: "solid",
transitions: [
{ name: "melt", from: "solid", to: "liquid" },
{ name: "freeze", from: "liquid", to: "solid" },
{ name: "vaporize", from: "liquid", to: "gas" },
{ name: "condense", from: "gas", to: "liquid" },
],
methods: {
onMelt: function () {
console.log("I melted");
},
onFreeze: function () {
console.log("I froze");
},
onVaporize: function () {
console.log("I vaporized");
},
onCondense: function () {
console.log("I condensed");
},
},
});
... which creates an object with a current state property:
fsm.state
... methods to transition to a different state:
fsm.melt()
fsm.freeze()
fsm.vaporize()
fsm.condense()
... observer methods called automatically during the lifecycle of a transition:
onMelt()
onFreeze()
onVaporize()
onCondense()
... along with the following helper methods:
fsm.is(s)
- return true if states
is the current statefsm.can(t)
- return true if transitiont
can occur from the current statefsm.cannot(t)
- return true if transitiont
cannot occur from the current statefsm.transitions()
- return list of transitions that are allowed from the current statefsm.allTransitions()
- return list of all possible transitionsfsm.allStates()
- return list of all possible states
Terminology
A state machine consists of a set of States
- solid
- liquid
- gas
A state machine changes state by using Transitions
- melt
- freeze
- vaporize
- condense
A state machine can perform actions during a transition by observing Lifecycle Events
- onBeforeMelt
- onAfterMelt
- onLeaveSolid
- onEnterLiquid
- ...
A state machine can also have arbitrary Data and Methods.
Multiple instances of a state machine can be created using a State Machine Factory.
Documentation
Read more about
- States and Transitions
- Data and Methods
- Lifecycle Events
- Asynchronous Transitions
- Initialization
- Error Handling
- State History
- Visualization
- State Machine Factory
- Upgrading from 2.x
Author
👤 keenzhang
- Github: @keenzhang
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a ⭐️ if this project helped you!
📝 License
Copyright © 2021 keenzhang.
This project is MIT licensed.