$ gnpm install fsm-base
Finite state machine.
Either mix it into an existing object:
import StateMachine from 'fsm-base'
const exampleClient = {}
StateMachine.mixInto(exampleClient, 'offline', [
{ from: 'offline', to: 'connecting' },
{ from: 'connecting', to: 'online' },
{ from: ['connecting', 'online'], to: 'offline' }
])
exampleClient._onStateChange = function (state, prevState) {
console.log(`Moved to ${state} from ${prevState}`)
}
console.log(exampleClient.state) // Prints 'offline' - the defined initial state
exampleClient.state = 'connecting' // valid move
exampleClient.state = 'online' // valid move
exampleClient.state = 'offline' // valid move
exampleClient.state = 'something unspecified' // invalid move, throws an exception.
..or define a class which extends it:
class AnotherClient extends StateMachine {}
const anotherClient = new AnotherClient()
anotherClient._initStateMachine('offline', [
{ from: 'offline', to: 'connecting' },
{ from: 'connecting', to: 'online' },
{ from: ['connecting', 'online'], to: 'offline' }
])
anotherClient.state = 'connecting'
// etc
string
The current state
Kind: instance property of StateMachine
Throws:
INVALID_MOVE
if an invalid move madeKind: instance method of StateMachine
Param | Type | Description |
---|---|---|
initialState | string |
Initial state, e.g. 'pending'. |
validMoves | Array.<object> |
Array of valid move rules. |
Invoked on every state change
Kind: instance method of StateMachine
Param | Type | Description |
---|---|---|
state | string |
the new state |
prevState | string |
the previous state |
Reset to initial state.
Kind: instance method of StateMachine
Kind: static method of StateMachine
Param | Type | Description |
---|---|---|
target | object |
The target to receive the state machine behaviour. |
initialState | string |
Initial state, e.g. 'pending'. |
validMoves | Array.<object> |
Array of valid move rules. |
© 2015-23 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.
Copyright 2013 - present © cnpmjs.org | Home |