https://github.com/75lb/fsm-base
Finite State Machine for use as a base class or mix-in
https://github.com/75lb/fsm-base
es6 finite-state-machine isomorphic javascript-library load-anywhere nodejs
Last synced: 8 days ago
JSON representation
Finite State Machine for use as a base class or mix-in
- Host: GitHub
- URL: https://github.com/75lb/fsm-base
- Owner: 75lb
- License: mit
- Created: 2015-10-25T19:48:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-07-06T13:32:41.000Z (10 months ago)
- Last Synced: 2025-03-27T13:12:30.254Z (26 days ago)
- Topics: es6, finite-state-machine, isomorphic, javascript-library, load-anywhere, nodejs
- Language: JavaScript
- Homepage:
- Size: 226 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/fsm-base)
[](https://www.npmjs.org/package/fsm-base)
[](https://github.com/75lb/fsm-base/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/fsm-base/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/fsm-base/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)# fsm-base
Finite state machine.
Either mix it into an existing object:
```js
import StateMachine from 'fsm-base'const device = {}
StateMachine.mixInto(device)device._initStateMachine('offline', [
{ from: 'offline', to: 'connecting' },
{ from: 'connecting', to: 'online' },
{ from: ['connecting', 'online'], to: 'offline' }
])device.onStateChange = function (state, prevState) {
console.log(state, prevState)
}device.state = 'connecting'
device.state = 'connecting' // should not trigger events again
device.state = 'online'
device.state = 'offline' // valid state move
```..or define a class which extends it:
```js
class Device extends StateMachine {}const device = new Device()
device._initStateMachine('offline', [
{ from: 'offline', to: 'connecting' },
{ from: 'connecting', to: 'online' },
{ from: ['connecting', 'online'], to: 'offline' }
])device.onStateChange = function (state, prevState) {
console.log(state, prevState)
}device.state = 'connecting'
// etc
```..or mix it into an existing class that does not `extend` fsm-base.
```js
class Device {}
StateMachine.mixInto(Device)const device = new Device()
device._initStateMachine('offline', [
{ from: 'offline', to: 'connecting' },
{ from: 'connecting', to: 'online' },
{ from: ['connecting', 'online'], to: 'offline' }
])device.onStateChange = function (state, prevState) {
console.log(state, prevState)
}device.state = 'connecting'
//etc
```See the [API Documentation](https://github.com/75lb/fsm-base/tree/master/docs/api.md) for more information.
* * *
© 2015-24 Lloyd Brookes \<[email protected]\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).