https://github.com/wheeyls/statemachine
https://github.com/wheeyls/statemachine
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/wheeyls/statemachine
- Owner: wheeyls
- License: mit
- Created: 2013-03-11T21:14:00.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2016-03-25T11:43:34.000Z (over 10 years ago)
- Last Synced: 2025-06-20T01:05:42.489Z (about 1 year ago)
- Language: JavaScript
- Size: 164 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
StateMachine.js
===============
A simple state machine library. Loosely based on the ruby library aasm.
Example
-------
### Node
var stateMachine = require('state-machine');
// do stuff
### Browser
(function (stateMachine) {
// do stuff
}(window.stateMachine));
### Define States and Events
var myStates = stateMachine(function () {
this.state('young', { initial: true })
.state('middleage')
.state('old')
.event('age', 'young', 'middleage')
.event('age', 'middleage', 'old')
.event('enlightened', ['young', 'middleage', 'old'], 'bliss')
;
});
console.log(myState.currentState()); // young
myStates.age();
console.log(myState.currentState()); // middleage
myStates.age();
console.log(myState.currentState()); // old
myStates.enlightened();
console.log(myState.currentState()); // bliss
API
---
Pick your poison:
### Callback
var myStates = stateMachine(function (builder) {
builder.state('young', { initial: true })
.event(...)
});
### Chain
var myStates = stateMachine();
myStates
.build()
.state('young', { initial: true })
.event()
...
Hooks
-----
var myStates = stateMachine();
myStates.build()
.state('young', { initial: true
, leave: function () { /* do stuff */ }
, enter: function () { /* do stuff */ }
});
myStates.onChange = function (currentStateName, previousStateName) {
// do stuff
};