New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

machineto

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

machineto

Minimal (neto) implementation of a finite state machine in javascript

latest
Source
npmnpm
Version
0.0.16
Version published
Maintainers
1
Created
Source

machineto

npm version npm downloads Bower version Built with Grunt Build Status coverage status coveralls status Dependency Status devDependency Status

NPM

Minimal (neto) implementation of a finite state machine in javascript

Please report any bugs or feature requests, thanks!

Overview

Generates a finite state machine. States can be defined, transitions to these states can be performed and parameters to the actions can be passed. Supports synchronous and asynchronous actions.

Node.js

npm install machineto
function action() {
    // Do Something
}
var machineto = require("machineto");
var sm = new machineto("state1", {
    "state1": { "event": { action: action, nextState: "state2" } },
    "state2": { "event": { action: action } }
}, {
    "logger": true
});
sm.fire("event");

Browser

<script type="text/javascript" src="path/to/machineto.min.js"></script>
<script type="text/javascript">
    function action() {
        // Do Something
    }
    var sm = new Machineto("state1", {
        "state1": { "event": { action: action, nextState: "state2" } },
        "state2": { "event": { action: action } }
    });
    sm.fire("event");
</script>

AMD/Require.js

require.config({
    paths: {
        "machineto": "path/to/machineto",
    }
});
define(["machineto"], function (machineto) {
    function action() {
        // Do Something
    }

    var sm = new machineto("state1", {
        "state1": { "event": { action: action, nextState: "state2" } },
        "state2": { "event": { action: action } }
    });
    sm.fire("event");
});

Web Worker

state-machine.js

importScripts("path/to/machineto.min.js");
function action() {
    // Do Something
}
var sm = new Machineto("state1", {
    "state1": { "event": { action: action, nextState: "state2" } },
    "state2": { "event": { action: action } }
}, {
    "logger": true
});
onmessage = function (event) {
    if (event.data.request &&
        event.data.request.name) {
        postMessage({
            "response": sm[event.data.request.name] &&
                        sm[event.data.request.name].apply(this, event.data.request.params)
        });
    }
};

example.html

<script type="text/javascript">
    var workerSM = new Worker("path/to/state-machine.js");

    workerSM.onmessage = function (event) {
        console.log("State Machine Worker said: " + JSON.stringify(event.data));
    };

    workerSM.postMessage({ request: {
        "name": "getCurrentState"
    }});
    workerSM.postMessage({ request: {
        "name": "fire",
        "params": [
                "event"
        ]
    }});
    workerSM.postMessage({ request: {
        "name": "getCurrentState"
    }});
</script>

Examples

A Quick Example

function on() {
    // Do Something
}
function off() {
    // Do Something
}
function allow() {
    // Do Something
}
function sleep(callback) {
    // Do Something
}
var sm = new Machineto("off", {
  "off": {
      "setCode": { action: allow },
      "turnOn": { action: on, nextState: "on" },
      "turnSleep": { action: sleep, async: true, nextState: "sleep" }
  },
  "on": {
      "setCode": { action: allow },
      "turnOff": { action: off, nextState: "off" }
  },
  "sleep": {
      "turnOn": { action: on, nextState: "on" },
  }
}, {
  "logger": console
});

sm.fire("setCode", "#1234");    // invokes allow("#1234") and returns true
sm.getCurrentState();           // returns "off" (current state)
sm.fire("turnOn", "now!");      // invokes on("now!") and returns true
sm.getCurrentState();           // returns "on" (current state)
sm.fire("turnOn", "check!");    // returns false (no action was called)
sm.getCurrentState();           // returns "on" (current state)
sm.fire("setCode", "1234#");    // invokes allow("1234#") and returns true
sm.getCurrentState();           // returns "on" (current state)
sm.fire("setCode", "#");        // invokes allow("#") and returns true
sm.getCurrentState();           // returns "on" (current state)
sm.fire("turnOff", "bye!");     // invokes off("bye!") and returns true
sm.getCurrentState();           // returns "off" (current state)
sm.fire("turnSleep", callback); // invokes sleep(callback) and returns true
sm.getCurrentState();           // returns "off" (current state)
sm.getCurrentState();           // returns "sleep" (current state) after callback is invoked

Note: for more examples look at the tests

Contributing

Find a bug? Have a feature request? Please create an Issue. If you find this project useful please consider "starring" it to show your support!

Author

Itai Koren (@itkoren) itkoren@gmail.com

Special thanks to @miki2826, for helping to design and create this piece

License

Copyright (c) 2014 Itai Koren (@itkoren) itkoren@gmail.com, contributors.

Releases

0.0.16 (2014-11-23)

Features
  • async: add support for async actions (5de84637)

0.0.15 (2014-11-22)

0.0.14 (2014-11-22)

Bug Fixes
  • dependencies: update package descriptors (802f55c5)
Features
  • build: added tasks to grunt file (f392e164)

This file was generated by grunt-verb on November 23, 2014.

Keywords

state machine

FAQs

Package last updated on 22 Nov 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts