Skip to content

Instantly share code, notes, and snippets.

@frame-lang
Last active Nov 16, 2018
Embed
What would you like to do?
//-------------------------------------------------//
// //
// Frame Machine Notation //
// //
//-------------------------------------------------//
// FMN Gist by Mark Truluck
/*
#DelayDemo
-interface-
start @(|>>|)
timeout
doAgain
-machine-
$Begin => $Default
|>>|
log("$Begin |>>|")
disableDoAgainBtn()
-> $Delaying ^
$Delaying
|>|
log("$Delaying |>|")
showDelayMessage()
initCounter()
startTimer() ^
|timeout|
log("$Delaying |timeout|")
decrementCounter()
isCounterAt0() ? -> $Ready :: ^
|<|
stopTimer()
log("$Delaying |<|") ^
$Ready
|>|
log("$Ready |>|")
addListeners()
enableDoAgainBtn()
showReadyMessage() ^
|doAgain|
log("$Ready |doAgain|")
-> $Delaying ^
|<|
disableDoAgainBtn()
removeListeners()
log("$Ready |<|") ^
*/
//-------------------------------------------------//
// //
// Implementation //
// //
//-------------------------------------------------//
var FrameEvent = function(message, data) {
var that = {};
that.message = message;
that.data = data;
that.return = null;
return that;
};
// #DelayDemo
var DelayDemo = function()
{
var that = {};
/********************
-interface-
start @(|>>|)
timeout
doAgain
********************/
that.start = function () {
var e = FrameEvent(">>");
_state_(e);
};
that.timeout = function () {
var e = FrameEvent("timeout");
_state_(e);
};
that.doAgain = function () {
var e = FrameEvent("doAgain");
_state_(e);
};
// -machine-
/********************
$Begin => $Default
|>>|
log("$Begin |>>|")
disableDoAgainBtn()
-> $Delaying ^
********************/
var _sBegin = function (e) {
if (e.message == ">>") {
log("$Begin |>>|");
disableDoAgainBtn();
_transition(_sDelaying);
return;
}
};
var _state_ = _sBegin;
/********************
$Delaying
|>|
log("$Delaying |>|")
showDelayMessage()
initCounter()
startTimer() ^
|timeout|
log("$Delaying |timeout|")
decrementCounter()
isCounterAt0() ? -> $Ready :: ^
|<|
stopTimer()
log("$Delaying |<|") ^
********************/
var _sDelaying = function (e) {
if (e.message == ">") {
log("$Delaying |>|");
showDelayMessage();
initCounter();
startTimer();
return;
}
if (e.message == "timeout") {
log("$Delaying |timeout|");
decrementCounter();
if (isCounterAt0()) {
_transition(_sReady);
return;
}
return;
}
if (e.message == "<") {
stopTimer();
log("$Delaying |<|");
return;
}
};
/********************
$Ready
|>|
log("$Ready |>|")
addListeners()
enableDoAgainBtn()
showReadyMessage() ^
|doAgain|
log("$Ready |doAgain|")
-> $Delaying ^
|<|
disableDoAgainBtn()
removeListeners()
log("$Ready |<|") ^
********************/
var _sReady = function (e) {
if (e.message == ">") {
log("$Ready |>|");
addListeners();
enableDoAgainBtn();
showReadyMessage();
return;
}
if (e.message == "doAgain") {
log("$Ready |doAgain|")
_transition(_sDelaying);
return;
}
if (e.message == "<") {
disableDoAgainBtn();
removeListeners();
log("$Ready |<|");
return;
}
};
// -actions-
var addListeners = function(msg) {
$("#doAgainBtn").on("click", that.doAgain);
};
var removeListeners = function(msg) {
$("#doAgainBtn").off("click", that.doAgain);
};
var showDelayMessage = function() {
$("#message").text("Delaying");
};
var initCounter = function() {
counter = 5;
$("#counter").text(counter);
};
var startTimer = function() {
timer = setInterval(that.timeout,1000);
};
var decrementCounter = function() {
counter--;
$("#counter").text(counter);
};
var isCounterAt0 = function() {
return counter == 0;
};
var stopTimer = function() {
clearInterval(timer);
timer = null;
};
var showReadyMessage = function() {
$("#message").html("Ready!");
};
var enableDoAgainBtn = function() {
$("#doAgainBtn").removeAttr("disabled");
};
var disableDoAgainBtn = function() {
$("#doAgainBtn").attr("disabled", "disabled");
};
var log = function(msg) {
console.log(msg);
};
// -domain-
var counter;
var timer;
// Machinery and mechanisms
var _transition = function(newState) {
_state_(FrameEvent("<"));
_state_ = newState;
_state_(FrameEvent(">"));
};
//-------------------------------------------------//
return that;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment