fsm-base
Finite state machine base class
Last updated 2 years ago by 75lb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm 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 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

Param Type Description
initialState string Initial state, e.g. 'pending'.
validMoves Array.<object> Array of valid move rules.

stateMachine._onStateChange(state, prevState)

Invoked on every state change

Kind: instance method of StateMachine

Param Type Description
state string the new state
prevState string the previous state

stateMachine.resetState()

Reset to initial state.

Kind: instance method of StateMachine

StateMachine.mixInto(target, initialState, validMoves)

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.

Current Tags

  • 0.8.0                                ...           latest (2 years ago)

15 Versions

  • 0.8.0                                ...           2 years ago
  • 0.7.0                                ...           3 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
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (4)

Copyright 2013 - present © cnpmjs.org | Home |