Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Last active December 19, 2015 17:38
Show Gist options
  • Select an option

  • Save srikumarks/5992336 to your computer and use it in GitHub Desktop.

Select an option

Save srikumarks/5992336 to your computer and use it in GitHub Desktop.
So called "CSP" using IO.js. Implements the "dramatic .. fall-out-of-your-chair" example shown in http://swannodette.github.io/2013/07/12/communicating-sequential-processes/
<div id="render"></div>
<!-- Take these from https://github.com/srikumarks/IO.js -->
<script src="IO.js"></script>
<script src="IO.Browser.js"></script>
<script>
// The rendering process that writes out the array passed to
// it into the div element with id = 'render'.
var render = IO.do([
function (arr) {
if (arr.length > 10) {
arr.splice(0, arr.length - 10);
}
return arr.map(function (proc) {
return '<div> Process ' + proc + '</div>';
}).join('');
},
IO.Browser.show('render', 'innerHTML')
]);
// The 'coordinator' process. collectUntil will accumulate
// inputs into an array and send the array to the rest of the
// chain.
var coord = IO.atomic([IO.collectUntil(), render]);
// Three "processes" all writing to the coordinator.
var p250 = IO.do([IO.clock(250), IO.supply(1), coord]);
var p1000 = IO.do([IO.clock(1000), IO.supply(2), coord]);
var p1500 = IO.do([IO.clock(1500), IO.supply(3), coord]);
// Tee off all the three processes.
IO.run('start', IO.do([p250, p1000, p1500].map(IO.tee)));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment