stately

0.1.2 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i stately

Weekly Downloads

10

Version

0.1.2

License

none

Last publish

Collaborators

  • jchris