0.8.0 • Published 1 year ago

fsm-base v0.8.0

Weekly downloads
265
License
MIT
Repository
github
Last release
1 year ago

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI js-standard-style

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

fsm-base

StateMachine ⏏

Kind: Exported class

stateMachine.state : string

The current state

Kind: instance property of StateMachine
Throws:

  • INVALID_MOVE if an invalid move made

stateMachine._initStateMachine(initialState, validMoves)

Kind: instance method of StateMachine

ParamTypeDescription
initialStatestringInitial state, e.g. 'pending'.
validMovesArray.<object>Array of valid move rules.

stateMachine._onStateChange(state, prevState)

Invoked on every state change

Kind: instance method of StateMachine

ParamTypeDescription
statestringthe new state
prevStatestringthe previous state

stateMachine.resetState()

Reset to initial state.

Kind: instance method of StateMachine

StateMachine.mixInto(target, initialState, validMoves)

Kind: static method of StateMachine

ParamTypeDescription
targetobjectThe target to receive the state machine behaviour.
initialStatestringInitial state, e.g. 'pending'.
validMovesArray.<object>Array of valid move rules.

© 2015-23 Lloyd Brookes \75pound@gmail.com\. Documented by jsdoc-to-markdown.

0.8.0

1 year ago

0.7.0

3 years ago

0.6.0

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.1

6 years ago

0.2.0

7 years ago

0.1.0

9 years ago