fsm-base
v0.9.0
Published
Finite state machine base class
Downloads
902
Maintainers
Readme
fsm-base
Finite state machine.
Either mix it into an existing object:
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:
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.
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 for more information.
© 2015-24 Lloyd Brookes <[email protected]>. Documented by jsdoc-to-markdown.