https://github.com/oldj/easy-fsm
An easy-to-use finite state machine system for JavaScript.
https://github.com/oldj/easy-fsm
Last synced: about 1 year ago
JSON representation
An easy-to-use finite state machine system for JavaScript.
- Host: GitHub
- URL: https://github.com/oldj/easy-fsm
- Owner: oldj
- License: mit
- Created: 2018-04-20T09:22:42.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-06T13:43:14.000Z (about 2 years ago)
- Last Synced: 2025-04-30T03:18:29.451Z (about 1 year ago)
- Language: TypeScript
- Size: 86.9 KB
- Stars: 13
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# easy-fsm
An easy-to-use finite state machine system for JavaScript.
## install
```bash
npm install easy-fsm
```
## usage
```TypeScript
import EasyFSM from 'easy-fsm'
let fsm = new EasyFSM({
initial: 'init',
states: {
init: {
on: {
loaded: 'ready'
}
},
ready: {}
}
})
console.log(fsm.getState()) // init
await fsm.fire('loaded')
console.log(fsm.getState()) // ready
```
## APIs
You can use `new EasyFSM(options)` to create a finite state machine.
- **send(event)**
Send an event on current state.
- **sendAndWait(event)**
Send an event on current state and wait for all listeners to finish.
- **canSend(event)**
Detect if the specified `event` can be sent.
- **[getter] state**
Get the current state.
- **[getter] previous_state**
Get the previous state.
- **onEnter(state, callback)**
Execute `callback` when entering `state`.
- **onLeave(state, callback)**
Execute `callback` when leaving `state`.