fsm-base
Finite state machine base class
Last updated 2 years ago by 75lb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install fsm-base 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.9.0                                ...           latest (2 years ago)

16 Versions

  • 0.9.0                                ...           2 years ago
  • 0.8.0                                ...           4 years ago
  • 0.7.0                                ...           5 years ago
  • 0.6.0                                ...           6 years ago
  • 0.5.2                                ...           7 years ago
  • 0.5.1                                ...           7 years ago
  • 0.5.0                                ...           7 years ago
  • 0.4.4                                ...           8 years ago
  • 0.4.3                                ...           8 years ago
  • 0.4.2                                ...           8 years ago
  • 0.4.1                                ...           8 years ago
  • 0.4.0                                ...           8 years ago
  • 0.3.0                                ...           8 years ago
  • 0.2.1                                ...           9 years ago
  • 0.2.0                                ...           10 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 47
Today 0
This Week 0
This Month 23
Last Day 0
Last Week 9
Last Month 1
Dependencies (1)
Dev Dependencies (4)

© 2010 - cnpmjs.org x YWFE | Home | YWFE