$ cnpm install fsm-base
const StateMachine = require('fsm-base')
class Stateful extends StateMachine {
constructor () {
super([
{ from: undefined, to: 'one' },
{ from: 'one', to: 'two' },
{ from: 'two', to: 'three' },
{ from: [ 'one', 'three' ], to: 'four'}
])
}
}
const instance = new Stateful()
instance.state = 'one' // valid state change
instance.state = 'two' // valid state change
instance.state = 'four' // throws - invalid state change
Emitter ⏏Kind: Exported class
Extends: Emitter
| Param | Type | Description |
|---|---|---|
| initialState | string |
Initial state, e.g. 'pending'. |
| validMoves | Array.<object> |
Array of valid move rules. |
stringThe current state
Kind: instance property of StateMachine
Throws:
INVALID_MOVE if an invalid move madeSet the current state. The second arg onward will be sent as event args.
Kind: instance method of StateMachine
| Param | Type |
|---|---|
| state | string |
Reset to initial state.
Kind: instance method of StateMachine
Emits: event:"reset"
fired on every state change
Kind: event emitted by StateMachine
| Param | Type | Description |
|---|---|---|
| state | string |
the new state |
| prev | string |
the previous state |
© 2015-21 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.
Copyright 2013 - present © cnpmjs.org | Home |