scxml

An implementation of SCXML in JavaScript.

npm install scxml
18 downloads in the last month

scxml.js is a lightweight SCXML-to-JavaScript compiler that targets the next generation of the SCION Statecharts interpreter. It currently supports node.js and the browser, and will later support Rhino and other JavaScript environments.

Note that scxml.js is still new and should be considered beta-quality software. For a more robust and debugged JavaScript SCXML interpreter, please see the current version of SCION.

Overview

scxml.js provides an implementation of the W3C SCXML draft specification in JavaScript. SCXML provides a declarative markup for Statecharts, a powerful modelling language for developing complex, timed, event-driven, state-based systems, and can offer elegant solutions to many problems faced in development of JavaScript-based applications across various domains. In the browser, SCXML can be used to facilitate the development of rich, web-based user interfaces with complex behavioural requirements. On the server, SCXML can be used to manage asynchronous control flow.

Installation

In node.js, install scxml.js via npm:

npm install scxml

In the browser, add the following script tags to your web page:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/es5-shim/1.2.4/es5-shim.min.js"></script>
<script type="text/javascript" src="http://jbeard4.github.com/scxml.js/builds/latest/scxml.js"></script>

Note that scxml.js assumes the presence of jQuery to handle cross-browser XMLHTTPRequest, however an alternative Ajax library could instead be used. This is set up in the following way:

    //perform this setup once, before SCION is used
    scxml.ext.platformModule.platform.ajax = {
        get : function(url,successCallback,dataType){
            //call your preferred Ajax library here to do HTTP GET
            //if dataType is 'xml', the Ajax response must be parsed as DOM
        },
        post : function(url, data, successCallback, dataType){
            //call your preferred Ajax library here to do HTTP POST
        }
    };

Support is currently being added for Rhino.

API

scxml.js uses SCION as its Statecharts engine. scxml.js first compiles the SCXML document to a JavaScript object model. The "model" is then used to instantiate a SCION Statecharts interpreter.

Here is an example of a typical usage:

    scxml.urlToModel(url,function(err, model){

        if(err) throw err;

        //you can inspect the generated code if you like using JavaScript's Function.prototype.toString
        console.log(model.toString());       

        //instantiate the interpreter
        var statechart1 = new scxml.scion.Statechart(model);

        //you can instantiate a second interpreter using the same model
        var statechart2 = new scxml.scion.Statechart(model);

        //start the interpreter
        var initialConfiguration = statechart1.start();

        //send events
        statechart1.gen({name : 'foo', data : 'bar'});
    });

In node, you can also use require.

var model = require('./path/to/foo.scxml');
//etc...

Note that this will only work if the SCXML

npm loves you