Javascript State Machine v2.1
Sat, Jan 7, 2012I have been neglecting my javascript state machine library for a few months while I wandered the world (of Skyrim!), and a number of feature requests have come in that now deserve a release - v2.1, that closes all the current outstanding github issues.
The code, along with updated usage instructions are available on github.
Enjoy!
New features include:
- Wildcard Events
- No-Op Events
- Custom Error Handler
- Cancelable
onleavestate
callback - Other Minor Changes
Wildcard Events (#11)
This release adds support for wildcard '*'
events that can be fired from any current state. E.g:
var fsm = StateMachine.create({
initial: 'stopped',
events: [
{ name: 'stop', from: '*', to: 'stopped' }
]});
Wildcard events can be specified using
'*'
, or by omiting thefrom
value.
No-Op Events (#5)
Added support for no-op events that wont transition the state if you omit the to
attribute. E.g:
var fsm = StateMachine.create({
initial: 'stopped',
events: [
{ name: 'start', from: 'ready', to: 'running' },
{ name: 'pause', from: 'running', to: 'paused' },
{ name: 'check', from: 'ready' /* no-op */ }
]});
This is a cosmetic shortcut for declaring the same
from
andto
values.
Custom Error Handler (#3, #10)
By default, if you try to call an event method that is not allowed in the current state, the
state machine will throw an exception. If you prefer to handle the problem yourself, you can
define a custom error
handler:
var fsm = StateMachine.create({
initial: 'stopped',
events: [
...
],
error: function(eventName, from, to, args, errorCode, errorMessage) {
// perform your own error handling here
},
});
Cancelable onleavestate
callback #13
The onleavestate
callback can now cancel the current event if it returns false
.
NOTE: this breaks backward compatibility for asynchronous transitions, which used to return
false
in order to trigger an asynchronous transition, but now need to returnStateMachine.ASYNC
instead offalse