Start with two modules salvaged from existing projects

This commit is contained in:
Tissevert 2019-01-12 20:07:31 +01:00
commit 06f0b21e85
2 changed files with 115 additions and 0 deletions

76
async.js Normal file
View File

@ -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);
};
}
}

39
dom.js Normal file
View File

@ -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;
}
}