v2.0.0
Bon-état will now generate whole objects instead of just the change function (as the previous version did). This should make it possible, and efficient, to have thousands of the same state machine running at the same time.
var FSM = require('bon-etat');
var turnstile = {
locked: {
coin: 'unlocked',
push: 'locked'
},
unlocked: {
push: 'locked',
coin: 'unlocked'
}
}
var Turnstile = FSM(turnstile);
var entrance = new Turnstile();
console.log(entrance.state); // 'locked'The state that the instance should be initialised with can be passed in during initialisation, ie. var entrance = new Turnstile('unlocked'); would initialise the instance in unlocked state.
The before functions has been removed. They did not seem necessary, as other transition events carried that information in their name or arguments.
Finally, we are now able to print the generated code for a state machine, by passing the definition to bonEtat.toString:
var bonEtat = require('bon-etat');
var turnstile = {
locked: {
coin: 'unlocked',
push: 'locked'
},
unlocked: {
push: 'locked',
coin: 'unlocked'
}
};
console.log(bonEtat.toString(turnstile)); // print the generated code