Skip to content

ok2ju/fsm-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Finite State Machine example

  • Loading -> trigger:Respond -> Ready
  • Ready -> trigger:Request -> Loading
// States: loading, ready
// Triggers: request, respond
const machine = FSM('loading', {
  loading: {
    respond: 'ready'
  },
  ready: {
    request: 'loading'
  }
})

// General subscribers
machine.subscribe((state) => {
  console.log('on state change', state)
})

// Subscribers by specific trigger
machine.subscribe((state) => {
  console.log('called on `request` trigger', state)
}, 'request')

machine.state // loading, initial state
machine.send('respond')
machine.state // ready

Releases

No releases published

Packages

No packages published