0.9.0 • Published 6 months ago

fsm-base v0.9.0

Weekly downloads
265
License
MIT
Repository
github
Last release
6 months 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 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.

0.9.0

6 months ago

0.8.0

2 years ago

0.7.0

4 years ago

0.6.0

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.1

7 years ago

0.2.0

8 years ago

0.1.0

9 years ago