JSFiddle - React, Tailwind, and code Playground
by ifandelse
HTML
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
JavaScript
var fsm = {
// There are probably better ways to do this, but the gist
// is to iterate through the "conditions" until a state is
// found that matches the current checklist (or the current
// state is used instead....
determineTransition: function() {
return _.reduce(this.transitionFlags, function(memo, v, k) {
console.log("checklist: " + JSON.stringify(this.checkList));
console.log(JSON.stringify(v.conditions));
if (!memo && _.isEqual(v.conditions, this.checkList)) {
return k;
}
return memo;
}, "", this) || this.state;
},
state: "stateA",
// Working check list of where things are
checkList: {
constraint1: true,
constraint2: true,
constraint3: false,
},
// the flags that determine when a state
// should be the transition target
transitionFlags: {
stateA: {
conditions: {
constraint1: false,
constraint2: false,
constraint3: false
}
},
stateB: {
conditions: {
constraint1: false,
constraint2: true,
constraint3: false
}
},
stateC: {
conditions: {
constraint1: true,
constraint2: true,
constraint3: false
}
}
}
};
document.write(fsm.determineTransition());