Skip to content

Instantly share code, notes, and snippets.

@coodoo
Last active December 10, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coodoo/4458600 to your computer and use it in GitHub Desktop.
Save coodoo/4458600 to your computer and use it in GitHub Desktop.
What if we can assign an action (callback) for each of the 'eat' event ? this way switch/case would be eliminated.
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'eat', from: 'hungry', to: 'satisfied', action: 'foo' },
{ name: 'eat', from: 'satisfied', to: 'full', action: 'bar' },
{ name: 'eat', from: 'full', to: 'sick', action: 'coo' },
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry', action: 'doo' },
]});
//action
fsm.foo = function( evt, from, to ){
//
}
fsm.bar = function( evt, from, to ){
//
}
fsm.coo = function( evt, from, to ){
//
}
fsm.doo = function( evt, from, to ){
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment