State Machine
Usage:
// create instance
var machine = new StateMachine.StateMachine({
verbose: true,
name: "MyStateMachine"
});
// define states
machine.state("state1")
.on("event1", function () {
// do something
this.go("state2");
})
.on("event2", function (x, y, z) {
// do something
console.log("x:", x, "y:", y, "z:", z);
})
.on("enter", function () {
// special event that gets run when the state is entered
});
machine.state("state2")
.on("event2", function () {
// do something else
this.go("state1");
})
.on("exit", function () {
// special event that gets run when the state is exited
});
// set initial state
machine.init("state1");
// invoke events
machine.emit("event1");
machine.emit("event2", "foo", "bar", "baz");
//get current state name
console.log(machine.current.name);