state-machine

Simple JavaScript State Machine Library

npm install state-machine
3 downloads in the last day
25 downloads in the last week
121 downloads in the last month
StateMachine.js
===============

A simple state machine library. Loosely based on the ruby library aasm.

Example
-------
### Node
    var stateMachine = require('state-machine');
    // do stuff

### Browser
    (function (stateMachine) {
      // do stuff
    }(window.stateMachine));

### Define States and Events

    var myStates = stateMachine(function () {
      this.state('young', { initial: true })
        .state('middleage')
        .state('old')
        .event('age', 'young', 'middleage')
        .event('age', 'middleage', 'old')
        .event('enlightened', ['young', 'middleage', 'old'], 'bliss')
        ;
    });

    console.log(myState.currentState()); // young
    myStates.age();
    console.log(myState.currentState()); // middleage
    myStates.age();
    console.log(myState.currentState()); // old
    myStates.enlightened();
    console.log(myState.currentState()); // bliss

API
---
Pick your poison:

### Callback

    var myStates = stateMachine(function (builder) {
      builder.state('young', { initial: true })
             .event(...)
    });

### Chain

    var myStates = stateMachine();

    myStates
      .build()
      .state('young', { initial: true })
      .event()
      ...

Hooks
-----
    var myStates = stateMachine();

    myStates.build()
      .state('young', { initial: true
                      , leave: function () { /* do stuff */ }
                      , enter: function () { /* do stuff */ }
                      });

    myStates.onChange = function (currentStateName, previousStateName) {
      // do stuff
    };
npm loves you