Javascript State Machine v1.2

Tue, Jun 21, 2011

Previously, I released a micro framework for a javascript finite state machine and was happy to hear from a number of people that used and liked it.

The main feedback was about the restriction that an event could only ever transition to one state. Rightly enough, there are legitimate uses for an event to transition to different states depending on the value of the current state.

So I took a little time out to tidy up the code, write a few more tests and add this missing functionality, released as version 1.2.0.

Multiple ’to’ states for a single event

If an event is allowed to transition to different states, depending on the current state, then provide multiple event entries with the same name:

  var fsm = StateMachine.create({
    initial: 'hungry',
    events: [
      { name: 'eat',  from: 'hungry',    to: 'satisfied' },
      { name: 'eat',  from: 'satisfied', to: 'full'      },
      { name: 'eat',  from: 'full',      to: 'sick'      }

The code, along with updated usage instructions are available on github.