https://github.com/wheeyls/statemachine
https://github.com/wheeyls/statemachine
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/wheeyls/statemachine
- Owner: wheeyls
- License: mit
- Created: 2013-03-11T21:14:00.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2016-03-25T11:43:34.000Z (about 9 years ago)
- Last Synced: 2024-12-09T04:15:46.789Z (6 months ago)
- Language: JavaScript
- Size: 164 KB
- Stars: 7
- Watchers: 2
- 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()); // blissAPI
---
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
};