jakesgordon / javascript-state-machine

A javascript finite state machine library
MIT License
8.69k stars 964 forks source link

State machine prototypes #76

Closed ssimeonov closed 7 years ago

ssimeonov commented 9 years ago

Great library!

What do you think about the create method being able to add methods to a constructor function and then expose a much simpler/faster instance creation method from that? It would be useful in the cases where an application has to manage many state machines.

jakesgordon commented 7 years ago

You should be able to do this already (see 'State Machine Classes' in the readme)

function MyClass() {
  this.startup();
}

StateMachine.create({
  target: MyClass.prototype,
  events: [
    { name: 'startup', from: 'none',  to: 'green' },
    { name: 'panic',   from: 'green', to: 'red' },
    // ...
  ]
});

I hope to make this a little easier (and ensure support for es6 syntax) in future v3