machineto
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
{ // Do Something}var machineto = ;var sm = "state1" "state1": "event": action: action nextState: "state2" "state2": "event": action: action "logger": true;sm;
Browser
AMD/Require.js
require;
;
Web Worker
state-machine.js
; { // Do Something}var sm = "state1" "state1": "event": action: action nextState: "state2" "state2": "event": action: action "logger": true; { if eventdatarequest && eventdatarequestname ; };
example.html
Examples
A Quick Example
{ // Do Something} { // Do Something} { // Do Something} { // Do Something}var sm = "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; // invokes allow("#1234") and returns truesm; // returns "off" (current state)sm; // invokes on("now!") and returns truesm; // returns "on" (current state)sm; // returns false (no action was called)sm; // returns "on" (current state)sm; // invokes allow("1234#") and returns truesm; // returns "on" (current state)sm; // invokes allow("#") and returns truesm; // returns "on" (current state)sm; // invokes off("bye!") and returns truesm; // returns "off" (current state)sm; // invokes sleep(callback) and returns truesm; // returns "off" (current state)sm; // 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.