Generator function called 3 times #2

Closed
breakbild opened this Issue Sep 25, 2012 · 1 comment

Projects

None yet

3 participants

I'm trying to write my first ignite program, and I'm running into something strange. It appears that the generator function is called 3 times. Why is that happening, am I missing something? It also happens when I use the ignite binary to run my state machine.

Code:
var testGen = function () {
console.log('gen');
return {
startState: 'step1',
states: {
step1: {
entry: function () {
console.log('entry');
return '@exit';
}
}
}
}
};
var factory = new ignite.Factory(testGen, {}, {});
factory.spawnWithCb(function () {
console.log('DONE');
});

Output:
gen
gen
gen
IGNT:StateMachine:Tue, 25 Sep 2012 18:3#1#0001:ENTR
IGNT:StateMachine:Tue, 25 Sep 2012 18:3#1#0001:CHNG: [@enter] -> [step1]
entry
IGNT:StateMachine:Tue, 25 Sep 2012 18:3#1#0001:CHNG: [step1] -> [@exit]
IGNT:StateMachine:Tue, 25 Sep 2012 18:3#1#0001:EXIT
DONE

Collaborator

Hi breakbild,

Thanks for trying out Ignite.

As you have discovered, the generator function is not only called once-per-spawn, but can also be called one or more times during the construction of the Factory(). This is because parts of the factory (e.g. the plug-in manager) actually need knowledge of the structure of the state machine.

You can actually reduce the number of calls to the function to one by giving the generator function a name, as, if it does not have one, it calls the generator function to see if the resulting object has a 'name' attribute.

var ignite = require('ignite');

var testGen = function testSM () { // Added a name to the function
  console.log('gen');
  return {
    startState: 'step1',
    states: {
      step1: {
        entry: function () {
          console.log('entry');
          return '@EXIT';
        }
      }
    }
  }
};

var factory = new ignite.Factory(testGen, {}, {});
console.log("After factory.") ;
factory.spawnWithCb(function () {
console.log('DONE');
});
console.log("After spawn.") ;

I thought we had mentioned this behaviour in the docs, but can't find it, so thanks for pointing this out as it should be clearly described. We should also clearly say that the generator function itself should not have any side effects (it should just return the state machine object), as it may be called an unknown number of times by the factory, and on these occasions the arguments will be blank.

@ignitejs ignitejs closed this Jun 8, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment