2019-01-12 20:07:31 +01:00
|
|
|
function Async() {
|
|
|
|
return {
|
2019-01-13 20:23:48 +01:00
|
|
|
apply: apply,
|
2019-01-12 20:07:31 +01:00
|
|
|
bind: bind,
|
2019-02-13 08:26:08 +01:00
|
|
|
fail: fail,
|
|
|
|
http: http,
|
|
|
|
map: map,
|
2019-01-12 20:07:31 +01:00
|
|
|
parallel: parallel,
|
|
|
|
run: run,
|
|
|
|
sequence: sequence,
|
|
|
|
wait: wait,
|
|
|
|
wrap: wrap
|
|
|
|
};
|
|
|
|
|
2019-01-13 20:23:48 +01:00
|
|
|
function apply(f, x) {
|
|
|
|
return function(g) {
|
|
|
|
g(f(x));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:26:14 +01:00
|
|
|
function bind() {
|
|
|
|
var m, steps, i;
|
|
|
|
if(arguments.length < 1) {
|
|
|
|
return wrap();
|
2019-01-12 20:07:31 +01:00
|
|
|
}
|
2019-01-13 20:26:14 +01:00
|
|
|
m = arguments[0];
|
|
|
|
steps = arguments;
|
|
|
|
i = 1;
|
|
|
|
return function(f) {
|
|
|
|
var step = function(x) {
|
|
|
|
if(i < steps.length) {
|
|
|
|
steps[i++](x)(step);
|
|
|
|
} else {
|
|
|
|
return f(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m(step);
|
|
|
|
};
|
2019-01-12 20:07:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-13 08:26:08 +01:00
|
|
|
function fail(message) {
|
|
|
|
return function(f) {
|
|
|
|
console.log(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function map(mapper) {
|
|
|
|
return function(x) {
|
|
|
|
return function(f) {
|
|
|
|
f(mapper(x));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:07:31 +01:00
|
|
|
function parallel() {
|
|
|
|
var threads = arguments;
|
|
|
|
var pending = threads.length;
|
|
|
|
var results = [];
|
2019-01-13 20:29:48 +01:00
|
|
|
var returned = [];
|
2019-01-12 20:07:31 +01:00
|
|
|
return function(f) {
|
|
|
|
var useResult = function(i) {
|
|
|
|
return function(x) {
|
2019-01-13 20:29:48 +01:00
|
|
|
if(!returned[i]) {
|
|
|
|
results[i] = x;
|
|
|
|
returned[i] = true;
|
|
|
|
pending--;
|
|
|
|
if(pending < 1) {
|
|
|
|
f(results);
|
|
|
|
}
|
2019-01-12 20:07:31 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-13 08:26:08 +01:00
|
|
|
function http(query) {
|
|
|
|
return function(f) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.addEventListener('load', function() {
|
|
|
|
f(this);
|
|
|
|
});
|
|
|
|
xhr.open(query.method, query.url);
|
|
|
|
if(query.method == 'POST') {
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
}
|
|
|
|
xhr.send(query.body);
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:07:31 +01:00
|
|
|
}
|