0.0.1 • Published 10 years ago
simplestates v0.0.1
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
- Stateless A C# Hierarchical State Machine
- Simple State Machine
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.
0.0.1
10 years ago
0.0.1-alpha
10 years ago