README
Va Hender?!
Va Hender?! is a JavaScript statemachine.
Those are the features:
- Simple syntax.
- States are hierarchical.
- There is a root state. All other states are descendants from the root.
- Unit tested code.
- "Va Hender?!" is Swedish for "What's Up?!", spelled like we pronounce it in Stockholm.
Get started
This is kind of how it works:
var
vh = require('vahender'),
rootState = new vh.Root();
// Define two child states to the root state:
var alive = rootState.child('alive');
var dead = rootState.child('dead');
// Define four child states to the alive state:
var hungry = alive.child('hungry');
var satisfied = alive.child('satisfied');
var full = alive.child('full');
var sick = alive.child('sick');
// Define events to cause transitions:
hungry.eat = satisfied.enter; // Call hungry.eat() to get to satisfied state.
satisfied.eat = full.enter; // Call satisfied.eat() to get to full state.
satisfied.rest = hungry.enter; // Call satisfied.rest() to get to hungry state.
full.eat = sick.enter; // Call full.eat() to get to sick state.
full.rest = satisfied.enter; // Call satisfied.rest() to get to satisfied state.
sick.eat = dead.enter; // Call sick.eat() to get to dead state.
sick.rest = full.enter; // Call satisfied.rest() to get to full state.
// dead state has no transitions.
// Define event to start it all with:
rootState.birth = hungry.enter;
var transition = rootState.birth(); // Returns a Transition object!
transition.on('started', function () {
console.log('Transition started.');
});
transition.on('aborted', function (reason) {
console.log('Transition was aborted: ' + reason);
});
transition.on('finished', function () {
console.log('Transition finished.');
});
// Add events to states:
full.on('enter', function() {
console.log('You are full.');
});
alive.update = function (next) {
// As long as alive (or any of alive's descendants) is the current state,
// this will be called (often!)
next(); // Must call the callback, or everything will stop.
};
State events
- enter - this state got activated.
- leave - this state got inactivated.
- newchild - this state got a new child. only emitted for children, not emitted for grandchildren. params: 1. the child state obj.
- current - this state is now the current state.
- childcurrent - some child became the current state. First argument is new the state object. Second argument is the old state object.
Note: There can only be one current state, but many activated states. When a state is current, it and all it's ancestors are activated.
Transition events
- started - this transition just started.
- aborted - this transition was aborted. First argument is the reason why (a string).
- finished - this transition just finished (successfully).
State properties
- .active - True if this state or any children/grandchildren/etc is the current state.
- .longname - The name of this state including breadcrubs from parent states.
- .name - The name of this state. (The string given at state creation.)
- .root - The root state object.
- .root.current - The current state.
- .parent - The parent of this state. Undefined if State is the root state.
- .parents - An array of parents, grand parents, and so on.
State methods
- .child(name, callback) - Creates a new child state. Returns the new child state object. If defined, the callback function will be called with the new child state object as the only argument.
- .hasParent(state) - True if this state is descendant to the given state. Otherwise false.
Transition properties
- .from - State to make transition from.
- .to - State to make transition to.
Change Log
- 1.1.0 - New feature: .child() has a callback.
- 1.0.1 - Bugfix: Only current state can initiate transition.
- 1.0.0 - Initial version.