Meteor package for workflow: machina.js Finite State Machine (FSM), client & server
A Finite State Machine is a computational abstraction that:
- Has a finite number of states in which it can exist
- Can only be in one state at any time
- Accepts input
- Can produce output determined by state &/or input
- Can transition from one state to another
Source: “Taming Complexity In JavaScript With Machina.js” by Jim Cowart
The global namespace 'machina' is available in both client & server contexts:
<template name="connection_tpl">
<div id="connection-div" class="col-sm-12 alert alert-success">
<div><label>Connection Status: </label>{{connection_status}}</div>
</div>
</template>
Deps.autorun(function () {
var connected = Meteor.status().connected;
Session.set("connected", connected);
});
Template.connection_tpl.rendered = function () {
var Div_Connection = $(this.find("#connection-div"));
var onlineFsm = new machina.Fsm({
initialState: "online",
states : {
online : {
_onEnter: function() {
Div_Connection.removeClass('alert-danger').addClass('alert-success');
},
},
offline : {
_onEnter: function() {
Div_Connection.removeClass('alert-success').addClass('alert-danger');
},
},
},
});
}
Template.connection_tpl.helpers({
connection_status: function () {
return Session.get("connected") ? "On" : "Off";
}
});