commit 06f0b21e851e5dafe76974280c301eaaff010af1 Author: Tissevert Date: Sat Jan 12 20:07:31 2019 +0100 Start with two modules salvaged from existing projects diff --git a/async.js b/async.js new file mode 100644 index 0000000..1205b00 --- /dev/null +++ b/async.js @@ -0,0 +1,76 @@ +function Async() { + return { + bind: bind, + parallel: parallel, + run: run, + sequence: sequence, + wait: wait, + wrap: wrap + }; + + function bind(m, f) { + return function(g) { + m(function(x) { + f(x)(g); + }); + } + } + + function parallel() { + var threads = arguments; + var pending = threads.length; + var results = []; + return function(f) { + var useResult = function(i) { + return function(x) { + results[i] = x; + pending--; + if(pending < 1) { + f(results); + } + }; + }; + for(var i = 0; i < threads.length; i++) { + threads[i](useResult(i)); + } + }; + } + + function run() { + var m; + if(arguments.length == 1) { + m = arguments[0]; + } else { + m = sequence.apply(null, arguments); + } + m(function() {}); + } + + function sequence() { + var steps = arguments; + var i = 0; + return function(f) { + var step = function(x) { + if(i < steps.length) { + steps[i++](step); + } else { + f(x); + } + } + step(); + }; + } + + function wait(delay) { + return function(f) { + setTimeout(f, delay); + }; + } + + function wrap(x) { + return function(f) { + f(x); + }; + } + +} diff --git a/dom.js b/dom.js new file mode 100644 index 0000000..649da2d --- /dev/null +++ b/dom.js @@ -0,0 +1,39 @@ +function Dom() { + return { + clear: clear, + make: make + } + + function clear(elem) { + while(elem.firstChild) { + elem.removeChild(elem.firstChild); + } + } + + function make(tag, properties, children) { + var e = document.createElement(tag); + properties = properties || {}; + children = children || []; + for(key in properties) { + var value = properties[key]; + switch(key) { + case "class": + e.className = Array.isArray(value) ? value.join(' ') : value; + break;; + case "maxlength": + e.setAttribute("maxlength", value); + break; + case "onClick": + e.addEventListener("click", value); + break;; + default: + e[key] = value; + } + } + for(var i = 0; i < children.length; i++) { + e.appendChild(children[i]); + } + return e; + } + +}