Jean-Jacques Dubray SAM Sample

Created by Jean-Jacques Dubray last modified
<!--
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
-->
<html>
<body>

<div id="representation">
	<blockquote>Problem is (follow me closely here, the science is pretty complicated), if I cut a hole in the Hab, the air won't stay inside anymore.</blockquote><br>
	- Andy Weir, The Martian
</div>

<script type="text/javascript">

////////////////////////////////////////////////////////////////////////////////
// Model 
//
const COUNTER_MAX = 10 ;

var model = {
		counter: COUNTER_MAX, 
		started: false,      
	    launched: false, 
	    aborted: false} ;

model.present = function(data) {		
	if (state.counting(model)) {
		if (model.counter === 0) {
			model.launched = data.launched || false ;
		} else {
			model.aborted = data.aborted || false ;
			if (data.counter !== undefined) { model.counter = data.counter ; }
		}
	} else {
		if (state.ready(model)) {
			model.started = data.started || false ;
		}
	}
	state.render(model) ;
}


////////////////////////////////////////////////////////////////////////////////
// View
//
var view = {} ;

// Initial State
view.init = function(model) {
	return view.ready(model) ;
}

// State representation of the ready state
view.ready = function(model) {
	return (
			"<p>Counter:"+model.counter+"</p>\n\
			<form onSubmit=\"JavaScript:return actions.start({});\">\n\
				<input type=\"submit\" value=\"Start\">\n\
			</form>"
		) ;

}

// State representation of the counting state
view.counting = function(model) {

	return (
			"<p>Count down:"+model.counter+"</p>\n\
			 <form onSubmit=\"JavaScript:return actions.abort({});\">\n\
				<input type=\"submit\" value=\"Abort\">\n\
			</form>"
		) ;

}

// State representation of the aborted state
view.aborted = function(model) {

	return (
			"<p>Aborted at Counter:"+model.counter+"</p>\n"
		) ;

}

// State representation of the launched state
view.launched = function(model) {

	return (
			"<p>Launched</p>"
		) ;

}

//display the state representation
view.display = function(representation) {
	var stateRepresentation = document.getElementById("representation");
	stateRepresentation.innerHTML = representation;
}

// Display initial state
view.display(view.init(model)) ;



////////////////////////////////////////////////////////////////////////////////
// State
//
var state =  { view: view} ;

model.state = state ;

// Derive the state representation as a function of the systen
// control state
state.representation = function(model) {
	var representation = 'oops... something went wrong, the system is in an invalid state' ;

	if (state.ready(model)) {
		representation = state.view.ready(model) ;
	} 

	if (state.counting(model)) {
		representation = state.view.counting(model) ;
	}

	if (state.launched(model)) {
		representation = state.view.launched(model) ;
	}

	if (state.aborted(model)) {
		representation = state.view.aborted(model) ;
	}

	state.view.display(representation) ;
}

// Derive the current state of the system
state.ready = function(model) {
	return ((model.counter === COUNTER_MAX) && !model.started && !model.launched && !model.aborted) ;
}

state.counting = function(model) {
	var status = ((model.counter <= COUNTER_MAX) && (model.counter >= 0) && model.started && !model.launched && !model.aborted) ;
	return status ;
}

state.launched = function(model) {
	return ((model.counter == 0) && model.started && model.launched && !model.aborted) ;
}

state.aborted = function(model) {
	return (
		(  model.counter <= COUNTER_MAX) && (model.counter >= 0) 
		&& model.started && !model.launched && model.aborted ) ;
}


// Next action predicate, derives whether
// the system is in a (control) state where
// an action needs to be invoked

state.nextAction = function (model) {
	if (state.counting(model)) {
		if (model.counter>0) {
			actions.decrement({counter: model.counter},model.present) ;
		}

		if (model.counter === 0) {
			actions.launch({},model.present) ;
		}
	}
}

state.render = function(model) {
	state.representation(model)
	state.nextAction(model) ;
}


////////////////////////////////////////////////////////////////////////////////
// Actions
//

var actions = {} ;

actions.start = function(data, present) {
	present = present || model.present ;
	data.started = true ;
	present(data) ;
	return false ;
}

actions.decrement = function(data, present) {
	present = present || model.present ;
	data = data || {} ;
	data.counter = data.counter || 10 ;
	var d = data ;
	var p = present ;
	setTimeout(function() {
		d.counter = d.counter - 1 ;
		p(d) ;
	}, 1000) ;
}

actions.launch = function(data, present) {
	present = present || model.present ;
	data.launched = true ;
	present(data) ;
}

actions.abort = function(data, present) {
	present = present || model.present ;
	data.aborted = true ;
	present(data) ;
	return false ;
}

</script>
</body>
</html> 

Comments (4)

  1. Ben Ellis

    Thanks for providing an example. I found it linked from your article at https://t.co/MrbuL8tFed There's a lot of interesting ideas here; you boil down what's valuable in react.js and improve on it while also cutting through the hype re GraphQL.

    A couple minor points re this example:

    1. tick() is never used, though it resembles actions.decrement().
    2. The onSubmit() expressions need to add "; return false" (or call event.preventDefault(), etc.). As they currently stand, clicking "start" winds up posting to the current page (reloading it) before the countdown can complete.
  2. Enguerrand Vidor

    Thank you Jean-Jacques for providing this example! The Javascript world is currently boiling and the path you are exploring seems to disrupt a couple of framework. I like it.