1.0.2 • Published 6 years ago

state-machine-flow v1.0.2

Weekly downloads
4
License
MIT
Repository
-
Last release
6 years ago

Stateflow

Lightweight module to execute tasks using a Finite State Machine (FSM).

npm install state-machine-flow

Usage

var StateMachine = require('state-machine-flow');

// Build StateMachine specifying two parameters:
// 1. Initial state
// 2. Object with event and an array of state transitions
var sm = new StateMachine('idle', {
  start: [ { from: 'idle', to: 'analyzing' } ],
  await: [ { from: 'analyzing', to: 'await-test-type' } ],
  cancel: [
    { from: ['analyzing', 'advisory'], to: 'idle' },
    { from: 'await-test-type', to: 'analyzing'},
    { from: 'result', to: 'remove-cartridge'}
  ],
  advisory: [ { from: '*', to: 'advisory' } ],
  result: [ { from: 'idle', to: 'result' } ],
  finish: [ { from: 'remove-cartridge', to: 'idle' } ]
})

// Add an event listener to execute every time the machine changes to the specified state.
sm.onEnter('analyzing', function () {
  console.log('analyzing started')
})

// Add an event listener to execute every time the machine leaves the specified state.
sm.onLeave('analyzing', function () {
  console.log('analyzing finished')
})

// Attach an error handler
sm.onError(function (err) {
  console.error('error', err)
})

// Execute the state change by specifying the event name
sm.trigger('start')

// Return the current state
console.log(sm.getCurrentState());

Test

$ npm test

License

MIT