Javascript State Machine v2.1(January 07, 2012)

I 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 the from 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 and to 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 return StateMachine.ASYNC instead of false

Other Minor Changes

  • closed issue #15 - support for loaders like require.js or curl.js
  • closed issue #12 - added explicit return values for event methods
  • closed issue #4, #14 - ensure before/after event hooks are called even if the event doesn’t result in a state change
  • fixed typos