Skip to content

Instantly share code, notes, and snippets.

@DmitryMyadzelets
Forked from podefr/LICENSE.txt
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save DmitryMyadzelets/c4619f96fb29e5b4a22f to your computer and use it in GitHub Desktop.

Select an option

Save DmitryMyadzelets/c4619f96fb29e5b4a22f to your computer and use it in GitHub Desktop.

State Machine Generator

Generates a state machine with the value you pass in parameter You can define states, transitions to these states, parameters to the actions, and execute the actions in a given context. This code was test driven developed with JsTestDriver, the TestCase is attached

Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke.nl/uglifyjs The example of the state machine comes from ... (need to find the url)

v1: only 85 bytes but the actions were ran in the global scope

v2: 128 bytes but the actions can be executed in a given context

/**
* Create a finite state machine
* @param {String} a the state on which the state machine will be initialized
* @param {Object} b the state machine's diagram :
* { "state1": {
* "event1": [action1, state2],
* "event2": [action2]
* },
* "state2": {
* "event3": [[action3, context], "state1"]
* }
* }
* @returns {Object}
*/
SM = function(
a // stores the current state
,b // an object to store all states and their transitions
){
return{
event:function( // The function to send an event to the state machine
c // The name of the event
,d // The arguments to pass to the action
,e
,f
){
e=b[a][c]; // Save the array [action, nextState] in e
if(e)return f=e[0], // If e is defined
f[0]&&f[0].call?f[0].call(f[1],d):f(d), // if call the function with the context or call it directly
a=e[1]||a // The next state is the new state and the new state is returned
}
}
}
function(a,b){return{event:function(c,d,e,f){e=b[a][c];if(e)return f=e[0],f[0]&&f[0].call?f[0].call(f[1],d):f(d),a=e[1]||a}}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Olivier Scherrer
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "StateMachineGenerator",
"description": "A state machine generator",
"keywords": [
"state",
"machine",
"generator",
"fsm",
"design pattern"
]
}
TestCase("SM", {
"test SM API": function() {
var sm = SM();
assertFunction(sm.event);
},
"test SM transits between 2 states": function() {
var open=function(){open.called=true;},
close=function(){close.called=true;},
sm = SM("closed",
{
// When in closed state, if the event is "open!" then execute the open action and go to opened state.
"closed":{"open!": [open, "opened"]},
// The opposite
"opened":{ "close!": [close, "closed"]}
});
assertUndefined(open.called);
assertEquals("opened", sm.event("open!"));
assertTrue(open.called);
assertUndefined(close.called);
assertEquals("closed", sm.event("close!"));
assertTrue(close.called);
},
"test SM doesn't crash if wrong event in given state": function() {
var sm = SM("state", {
"state": {"event": [function(){}, "nextState"]}
});
assertUndefined(sm.event("crash!"));
},
"test SM can pass arguments to the action": function () {
var action = function(arg) {action.arg = arg; },
sm = SM("state", {
"state": {"event": [action, "state2"]},
"state2": {"event": [action]}
}),
arg = {},
arg2 = {};
sm.event("event", arg);
assertSame(arg, action.arg);
sm.event("event", arg2);
assertSame(arg2, action.arg);
},
"test SM can run actions in given context": function () {
var action = function() { action.ctx = this; },
ctx = {},
sm = SM("state", {
"state": { "event": [[action, ctx], "nextState"]}});
sm.event("event");
assertSame(ctx, action.ctx);
},
"test the whole diagram": function() {
var alarm = function () { alarm.called = true; alarm.args = arguments; },
open = function () { open.called = true; open.args = arguments; },
thank = function () { thank.called = true; thank.args = arguments; },
close = function () { close.called = true; close.args = arguments; },
sm = SM("closed", {
"closed": {
"pass": [alarm],
"coin": [open, "opened"]
},
"opened": {
"coin": [thank],
"pass": [close, "closed"]
}
});
assertEquals("closed", sm.event("pass", "picture"));
assertTrue(alarm.called);
assertEquals("picture", alarm.args[0]);
assertEquals("opened", sm.event("coin", "$2"));
assertTrue(open.called);
assertEquals("$2", open.args[0]);
assertEquals("opened", sm.event("coin", "c50"));
assertTrue(thank.called);
assertEquals("c50", thank.args[0]);
assertEquals("closed", sm.event("pass"));
assertTrue(close.called);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment