fsm-base

0.9.0 • Public • Published

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 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 <75pound@gmail.com>. Documented by jsdoc-to-markdown.

Package Sidebar

Install

npm i fsm-base

Weekly Downloads

53

Version

0.9.0

License

MIT

Unpacked Size

15.6 kB

Total Files

6

Last publish

Collaborators

  • 75lb