extra-async-function
Version:
An async function is a function that delivers its result asynchronously (through Promise).
233 lines (229 loc) • 5.91 kB
JavaScript
;
function COMPARE$1(a, b) {
return a < b ? -1 : (a > b ? 1 : 0);
}
function name(x) {
return x.name;
}
function length(x) {
return x.length;
}
function bind(x, ths, ...prefix) {
return x.bind(ths, ...prefix);
}
function call(x, ths = null, ...args) {
return x.call(ths, ...args);
}
function apply(x, ths = null, args) {
return x.apply(ths, args);
}
function isAsync(v) {
const AsyncFunction = (async function () { }).constructor;
return v instanceof AsyncFunction;
}
function isGenerator(v) {
const GeneratorFunction = (function* () { }).constructor;
return v instanceof GeneratorFunction;
}
function contextify(x) {
return function (...args) { return x(this, ...args); };
}
function decontextify(x) {
return (ths = null, ...args) => x.call(ths, ...args);
}
function reverse(x) {
return (...args) => x(...args.reverse());
}
function spread(x) {
return (...args) => x(args);
}
function unspread(x) {
return (args) => x(...args);
}
function attach(x, ...prefix) {
return (...args) => x(...prefix, ...args);
}
function attachRight(x, ...suffix) {
return (...args) => x(...args, ...suffix);
}
function curry(x, n = x.length) {
return (...args) => {
if (args.length >= n)
return x(...args);
else
return curry((...rest) => x(...args, ...rest), n - args.length);
};
}
function curryRight(x, n = x.length) {
return curry(reverse(x), n);
}
function defer(x) {
return (...args) => {
var h = setImmediate(flush);
function clear() { clearImmediate(h); h = null; }
function flush() { x(...args); clear(); }
return { clear, flush };
};
}
function delay(x, t) {
return (...args) => {
var h = setTimeout(flush, t);
function clear() { clearTimeout(h); h = null; }
function flush() { x(...args); clear(); }
return { clear, flush };
};
}
function restrict(x, start, end = -1) {
var i = -1;
return (...args) => {
if ((++i < start) === (i < end || end < 0))
return;
return x(...args);
};
}
function restrictOnce(x) {
return restrict(x, 0, 1);
}
function restrictBefore(x, n) {
return restrict(x, 0, n);
}
function restrictAfter(x, n) {
return restrict(x, n);
}
function debounce(x, t, T = -1) {
var savedArgs;
var h = null, H = null;
function clear() {
clearTimeout(h);
clearTimeout(H);
h = H = null;
}
function flush() { x(...savedArgs); clear(); }
return (...args) => {
savedArgs = args;
if (T >= 0)
H = H || setTimeout(flush, T);
if (T < 0 || t < T) {
clearTimeout(h);
h = setTimeout(flush, t);
}
return { clear, flush };
};
}
function debounceEarly(x, t, T = -1) {
var h = null, H = null;
function clear() { h = H = null; }
function flush() { clear(); }
return (...args) => {
if (!h && !H)
x(...args);
if (T >= 0)
H = H || setTimeout(flush, T);
if (T < 0 || t < T) {
clearTimeout(h);
h = setTimeout(flush, t);
}
return { clear, flush };
};
}
function throttle(x, t) {
var savedArgs;
var h = null;
function clear() { h = null; }
function flush() { x(...savedArgs); clear(); }
return (...args) => {
savedArgs = args;
h = h || setTimeout(flush, t);
return { clear, flush };
};
}
function throttleEarly(x, t) {
var h = null;
function clear() { h = null; }
function flush() { clear(); }
return (...args) => {
if (!h)
x(...args);
h = h || setTimeout(flush, t);
return { clear, flush };
};
}
function ARGUMENTS(...args) {
return Promise.all(args);
}
async function NOOP(...args) { }
async function IDENTITY(v) {
return v;
}
async function COMPARE(a, b) {
return COMPARE$1(await a, await b);
}
function negate(x) {
return async (...args) => !(await x(...args));
}
function memoize(x, fr = null, cache = null) {
var fr = fr || IDENTITY;
var cache = cache || new Map();
return async (...args) => {
var k = await fr(...args);
if (cache.has(k))
return cache.get(k);
var v = await x(...args);
cache.set(k, v);
return v;
};
}
function compose(...xs) {
return composeRight(...xs.reverse());
}
function composeRight(...xs) {
return async (...args) => {
if (xs.length === 0)
return;
var a = await xs[0](...args);
for (var i = 1, I = xs.length; i < I; i++)
a = await xs[i](a);
return a;
};
}
exports.ARGUMENTS = ARGUMENTS;
exports.COMPARE = COMPARE;
exports.IDENTITY = IDENTITY;
exports.NOOP = NOOP;
exports.after = restrictAfter;
exports.apply = apply;
exports.arity = length;
exports.attach = attach;
exports.attachRight = attachRight;
exports.before = restrictBefore;
exports.bind = bind;
exports.call = call;
exports.compose = compose;
exports.composeRight = composeRight;
exports.contextify = contextify;
exports.curry = curry;
exports.curryRight = curryRight;
exports.debounce = debounce;
exports.debounceEarly = debounceEarly;
exports.decontextify = decontextify;
exports.defer = defer;
exports.delay = delay;
exports.flip = reverse;
exports.is = isAsync;
exports.isGenerator = isGenerator;
exports.length = length;
exports.memoize = memoize;
exports.name = name;
exports.negate = negate;
exports.once = restrictOnce;
exports.partial = attach;
exports.partialRight = attachRight;
exports.restrict = restrict;
exports.restrictAfter = restrictAfter;
exports.restrictBefore = restrictBefore;
exports.restrictOnce = restrictOnce;
exports.reverse = reverse;
exports.spread = spread;
exports.throttle = throttle;
exports.throttleEarly = throttleEarly;
exports.unspread = unspread;