@holymoly/state

1.0.2 • Public • Published

state

A simple state model with transition control

Example

const State = require('@holymoly/state');

// states allowed
var stateModel = [
  'idle',
  'created',
  'running',
  'completed',
  'hold',
  'aborted',
  'error'
];

//transitions allowed
var transitionModel = {
  'allowedTransitions': {
    'transitions': [{
      'actualState': 'idle',
      'newState': 'created'
    }, {
      'actualState': 'idle',
      'newState': 'aborted'
    }, {
      actualState: 'created',
      'newState': 'running'
    }, {
      'actualState': 'created',
      'newState': 'aborted'
    }, {
      'actualState': 'running',
      'newState': 'completed'
    }, {
      'actualState': 'running',
      'newState': 'hold'
    }, {
      'actualState': 'running',
      'newState': 'aborted'
    }, {
      'actualState': 'hold',
      'newState': 'running'
    }, {
      'actualState': 'error',
      'newState': '*'
    }, {
      'actualState': '*',
      'newState': 'error'
    }]
  }
};

// create new instance
var taskState = new State(stateModel, 'idle', transitionModel);

// state change is monitored by stateChange event
taskState.on('stateChange', function(data) {
  console.log(data);
})

// errors are raising error event
taskState.on('error', function(data) {
  console.log(data);
})

//set to create
taskState.setStateValue('created');
console.log(taskState.getStateValue());

// state idleasdfasdf is not defined
taskState.setStateValue('idleasdfasdf');
console.log(taskState.getStateValue());

// create to idl is not allowed
taskState.setStateValue('idle');
console.log(taskState.getStateValue());

// set to error
taskState.setStateValue('error');
console.log(taskState.getStateValue());

// error to running allowed wit wild card
taskState.setStateValue('running');
console.log(taskState.getStateValue());

// set to idle is not allowed
taskState.setStateValue('idle');
console.log(taskState.getStateValue());

// set to aborted
taskState.setStateValue('aborted');
console.log(taskState.getStateValue());

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.20latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @holymoly/state

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

1.05 MB

Total Files

35

Last publish

Collaborators

  • holymoly