Define a state transition
var fsm = new IgeFSM();
// Define an "idle" state
fsm.defineState('idle', {
enter: function (data, completeCallback) {
console.log('entered idle state');
completeCallback();
},
exit: function (data, completeCallback) {
console.log('exited idle state');
completeCallback();
}
});
// Define a "moving" state
fsm.defineState('moving', {
enter: function (data, completeCallback) {
console.log('entered moving state');
completeCallback();
},
exit: function (data, completeCallback) {
console.log('exited moving state');
completeCallback();
}
});
// Define a transition between the two methods
fsm.defineTransition('idle', 'moving', function (data, callback) {
// Check some data we were passed
if (data === 'ok') {
// Callback the listener and tell them there was no error
// (first argument is an err flag, set to false for no error)
callback(false);
} else {
// Callback and say there was an error by passing anything other
// than false in the first argument
callback('Some error string, or true or any data');
}
});
// Now change states and cause it to fail
fsm.enterState('moving', 'notOk', function (err, data) {
if (!err) {
// There was no error, the state changed successfully
console.log('State changed!', fsm.currentStateName());
} else {
// There was an error, the state did not change
console.log('State did NOT change!', fsm.currentStateName());
}
});
// Now change states and pass "ok" in the data to make it proceed
fsm.enterState('moving', 'ok', function (err, data) {
if (!err) {
// There was no error, the state changed successfully
console.log('State changed!', fsm.currentStateName());
} else {
// There was an error, the state did not change
console.log('State did NOT change!', fsm.currentStateName());
}
});