Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
CommonJS State Machine
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
README.md
package.json

README.md

Stately

A CommonJS state-machine that works in Node or the browser.

This is extracted from Twebz and is designed to DRY out the CouchDB changes listner Twebz's bot uses for handling Twitter API interactions. That said, this is entirely agnostic to backends and should work great with Redis, message queues, or anything else where you can get a feed of updates as they are happening to the database.

It is totally isolated from IO, so it will also run fine in the browser, although I haven't done that yet.

Usage

var stately = require("stately")
  , myMachine = stately.define({
    foo : function(obj) {
      // this function will be called if obj.state == "foo"
      obj.fooRan = true;
    }
  })
  , obj = {state : "foo", fooRan : false};

myMachine.handle(obj);

assert.isTrue(obj.fooRan);

In real life you would hook myMachine.handle up as the listener to events on a Node.js stream. Or in the browser as the callback to an event system or a comet feed.

You can also deal with objects of multiple types if you want, by nesting the definitions one-layer deep, see the tests for details.

The _getState and _getType methods are pluggable.

Tests

Haters gonna hate, but I used Vows and "it works!"

To run them:

npm install vows
vows test/test.js

TODO

  • Test modifying the states after definition
  • Document more usage scenarios

License

Apache 2.0

Something went wrong with that request. Please try again.