jakesgordon / javascript-state-machine

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

expose initial and have a reset functionality #114

Closed phoet closed 7 years ago

phoet commented 7 years ago

i would like to add two small features to the library to make my life a bit easier when testing.

a) expose the initial configuration option (or the config as a whole) b) make it possible to call reset() on a state-machine and bring it back to zero instead of having to initialize a new instance

what do you think?

jakesgordon commented 7 years ago

While it can be useful for testing purposes, I'm generally against the idea of a general purpose reset method because it won't fire the full transition event lifecycle and has the potential to leave the FSM (or the resources it manages) in an inconsistent state.

In the v3 branch (releasing to npm shortly) you can make a reset transition that respects the full lifecycle using a wildcard from and dynamic to transition:

init: 'none',
transitions: [
  { name: 'reset', from: '*', to: function() { return 'none' }
  ...
]

Note also, while not recommended, you can access the underlying original configuration if desired:

init: 'none',
transitions: [
  { name: 'reset', from: '*', to: function() { return this._fsm.config.init.state; }
  ...
]