simplestates

0.0.1 • Public • Published

SimpleStates

Simple state machines in JavaScript.

Installation

Via npm on Node:

npm install simplestates

Usage

Reference in your program:

var ss = require('simplestates');

Create state machine with initial state

var sm = ss.stateMachine('OffHook');

Define states with trigger actions

sm.state('OffHook')
    .when('CallDialed', 'Ringing');
    
sm.state('Ringing')
    .when('HungUp', 'OffHook')
    .when('CallConnected', 'Connected');

Define states with enter and exit functions

var exits = 0;
var ringings = 0;

sm.state('OffHook')
    .when('CallDialed', 'Ringing')
    .exit(function () { exits++; });
    
sm.state('Ringing')
    .enter(function () { ringings++; })
    .when('HungUp', 'OffHook')
    .when('CallConnected', 'Connected');

Define triggers with functions

var ringings = 0;

sm.state('OffHook')
    .when('CallDialed', 
        function () { ringings++; }, 
        'Ringing');

If a triggered function returns false the actions are stopped, and the next defined actions for the same trigger are executed

var calls = 0;

// ....
    
sm.state('Ringing')
    .when('HungUp', 'OffHook')
    .when('CallConnected', 
        function ()  { if (calls < 100) return false; },
        'Stop');
    .when('CallConnected', 
        function ()  { call++; }, 
        'Connected');

Usage in browser

<script src='simplestates.js' type='text/javascript'></script>

Then the simplestates object is defined:

var machine = simplestates.stateMachine('OffHook');

You can directly use the file lib/simplestates.js. It is already prepared for browser consumption.

Development

git clone git://github.com/ajlopez/SimpleStates.git
cd SimpleStates
npm install
npm test

Samples

TBD

References

License

MIT

Versions

  • 0.0.1 Published

Contribution

Feel free to file issues and submit pull requests � contributions are welcome<

If you submit a pull request, please be sure to add or update corresponding test cases, and ensure that npm test continues to pass.

Package Sidebar

Install

npm i simplestates

Weekly Downloads

1

Version

0.0.1

License

none

Last publish

Collaborators

  • ajlopez