1.3.1 • Published 16 days ago

stateinator v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
16 days ago

state-machine

Usage

  • Instantiation
  const StateMachine = require('stateinator')

  const context = {}

  const S = new StateMachine({})
  • Setup States
  S.addState('ONE', {
    onEnter: ()=>{
      thing.activate()
    },
    onUpdate: ()=>{
      thing.update()
    },
    onExit: ()=>{
      thing.deactivate()
    },
  })
  • Setup Transitions
  S.addTransition('ONE', 'TWO')
  S.addTransition('ONE', 'THREE')
  S.addTransition('TWO', 'ONE')
  S.addTransition('THREE', 'TWO')
  • Updating
  S.update()

update will perform any pending transition changes (1 per cycle) and call onUpdate for the current state.

  • Goto States
  S.setState('blah')
  
  onButtonClick = ()=> S.goto('flarn')

It is important to call setState initially. The default state is 'void' which is probably not useful. A transition from 'void' is always valid. Note, also, that a transition from a state to itself must be defined.

If you call goto while the machine is changing states then the transition will be queued. In order to proceed with enqueued transitions you need to periodically call update.

TODO

  • ?Should transitions also be Map?
1.3.1

16 days ago

1.3.0

1 year ago

1.2.1

3 years ago

1.2.0

3 years ago

1.0.0

3 years ago