npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

machineto

v0.0.16

Published

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

Downloads

12

Readme

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) [email protected]

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

License

Copyright (c) 2014 Itai Koren (@itkoren) [email protected], 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.