Skip to content

Latest commit

 

History

History
53 lines (50 loc) · 1.08 KB

README.md

File metadata and controls

53 lines (50 loc) · 1.08 KB

von

Finite State Machine

var fsm = require('von');
var consumption = fsm([
  { action: 'eat', from: 'hungry', to: 'satisfied'},
  { action: 'eat', from: 'satisfied', to: 'full' },
  { action: 'eat', from: 'full', to: 'sick' },
  { action: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry' }
]);
var bug = consumption('hungry', {
  onrest: function(action, from, to) {
    console.log('Phew');
  },
  exithungry: function(action, from, to, food) {
    console.log("I can survive, ate " + food);
  },
  entersick: function(action, from, to, food) {
    console.log(food + " was too much!");
  }
});
console.log(bug.state);
bug.eat('grass');
console.log(bug.state);
bug.eat('grass');
console.log(bug.state);
bug.eat('grass');
console.log(bug.state);
bug.rest();
console.log(bug.actions());
console.log(bug.is('hungry'));
console.log(bug.can('eat'));
console.log(bug.cannot('rest'));
console.log(bug.transitions());

Output:

hungry
I can survive, ate grass
satisfied
full
grass was too much!
sick
Phew
['eat', 'rest']
true
true
false
{'eat': 'satisfied', 'rest': 'hungry'}