Skip to content

v3.0.0

Latest

Choose a tag to compare

@gausby gausby released this 07 Oct 17:43

Breaking change: The state names in transition events are now written in capital case.

var FSM = require('bon-etat');
var Door = FSM({
    opened: {
        close: 'closed'
    },
    closed: {
        open: 'opened',
        break: 'broken beyond repair'
    },
    'broken beyond repair': {}
});

When going from the closed-state to the broken beyond repair state, the event would previously be written frontDoor.on('goingFromClosedToBroken beyond repair', function() {}); now it should be written as frontDoor.on('goingFromClosedToBrokenBeyondRepair', function() {});.

var frontDoor = new Door();

frontDoor.on('goingFromClosedToBrokenBeyondRepair', function() {
    console.log('call the repair man!');
});

frontDoor.change('close');
frontDoor.change('break');
// output: 'call the repair man!'

Also, when a state machine enters a state with no possible states to transition to (ie, the 'broken beyond repair' state in the previous example), it will emit final (function(from)), if the state machine receives further stimulus it will emit inFinal (function(state)).

var backDoor = new Door();
backDoor.on('final', function(from) {
    console.log('we got here from %s', from);
});

backDoor.on('inFinal', function(state) {
    console.log('the door is still %s', state);
});

backDoor.change('close');
backDoor.change('break');
// output 'we got here from closed'
backDoor.change('open');
// output 'the door is still broken beyond repair'