IgeFSM

Method: defineTransition

Defines a transition between two states.
defineTransition (String fromState, String toState, Function transitionCheck)
  • StringfromState The state name the transition is from.
  • StringtoState The state name the transition is to.
  • FunctiontransitionCheck A method to call just before this transition between the two specified states is executed, that will call the callback method passed to it in the second parameter and include either true to allow the transition to continue, or false to cancel it in the first parameter.
Returns *

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());
    }
});
© Copyright 2013 Irrelon Software Limited. All Rights Reserved. UK Registered Company Number: 07522767
Isogenic (ī´sōjen´ik): Adj originating from a common source; possessing the same genetic composition.
Strange Things Happen at the One Two Point