Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

fsm-base

0.6.0 • Public • Published

view on npm npm module downloads Gihub repo dependents Gihub package dependents Build Status js-standard-style

fsm-base

const StateMachine = require('fsm-base')
 
class Stateful extends StateMachine {
  constructor () {
    super([
      { from: undefined, to: 'one' },
      { from: 'one', to: 'two' },
      { from: 'two', to: 'three' },
      { from: [ 'one', 'three' ], to: 'four'}
    ])
  }
}
const instance = new Stateful()
instance.state = 'one'  // valid state change
instance.state = 'two'  // valid state change
instance.state = 'four' // throws - invalid state change

fsm-base

StateMachine ⇐ Emitter

Kind: Exported class
Extends: Emitter

new StateMachine(initialState, validMoves)

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

stateMachine.state : string

The current state

Kind: instance property of StateMachine
Throws:

  • INVALID_MOVE if an invalid move made

stateMachine.setState(state)

Set the current state. The second arg onward will be sent as event args.

Kind: instance method of StateMachine

Param Type
state string

stateMachine.resetState()

Reset to initial state.

Kind: instance method of StateMachine
Emits: event:"reset"

"state" (state, prev)

fired on every state change

Kind: event emitted by StateMachine

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

© 2015-20 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.

Install

npm i fsm-base

DownloadsWeekly Downloads

390

Version

0.6.0

License

MIT

Unpacked Size

26.6 kB

Total Files

7

Last publish

Collaborators

  • avatar