alinex-async
Version:
An extended version of the async library.
183 lines (173 loc) • 5.53 kB
JavaScript
(function() {
var chalk, debug, functionId, util;
debug = require('debug')('async:once');
chalk = require('chalk');
util = require('util');
functionId = 0;
module.exports["throw"] = function(context, func) {
var called;
if (!func) {
func = context;
context = void 0;
}
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
called = false;
if (func.__id == null) {
func.__id = ++functionId;
}
debug("throw #" + func.__id + ": created for " + (chalk.grey(func)));
if (context) {
debug("throw #" + func.__id + ": using specific context");
}
return function() {
debug("throw #" + func.__id + ": called");
if (called) {
debug("throw #" + func.__id + ": called again issuing an error");
throw new Error("This function should only be called once.");
}
called = true;
return func.apply(context, arguments);
};
};
module.exports.skip = function(context, func) {
var called;
if (!func) {
func = context;
context = void 0;
}
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
called = false;
if (func.__id == null) {
func.__id = ++functionId;
}
debug("skip #" + func.__id + ": created for " + (chalk.grey(func)));
if (context) {
debug("skip #" + func.__id + ": using specific context");
}
return function() {
var cb, err;
cb = arguments[arguments.length - 1];
if (called) {
debug("skip #" + func.__id + ": skipped because already called");
err = new Error("This function should only be called once.");
if (typeof cb === 'function') {
return cb(err);
}
return err;
}
debug("skip #" + func.__id + ": called");
called = true;
return func.apply(context, arguments);
};
};
module.exports.time = function(context, func) {
var listeners, next, nfunc, started;
if (!func) {
func = context;
context = void 0;
}
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
started = false;
listeners = [];
next = [];
if (func.__id == null) {
func.__id = ++functionId;
}
debug("time #" + func.__id + ": created for " + (chalk.grey(func)));
if (typeof context === 'object' && Object.keys(context).length) {
debug("time #" + func.__id + ": using specific context " + (chalk.grey(util.inspect(context))));
}
nfunc = function() {
var args, cb, idargs, ref;
args = [].slice.call(arguments);
cb = (ref = args.pop()) != null ? ref : {};
idargs = util.inspect(args.slice(0)).replace(/\n\s*/g, ' ');
if (started) {
debug("time #" + func.__id + ": " + (chalk.grey(idargs)) + " called but waiting");
next.push([idargs, args, cb]);
return;
} else {
debug("time #" + func.__id + ": " + (chalk.grey(idargs)) + " called");
listeners.push(cb);
}
started = true;
args.push(function() {
var i, id, len, ref1, work;
debug("time #" + func.__id + ": " + (chalk.grey(idargs)) + " done with result " + (chalk.grey(util.inspect(arguments))));
started = false;
work = [].slice.call(listeners);
listeners = [];
for (i = 0, len = work.length; i < len; i++) {
cb = work[i];
debug("time #" + func.__id + ": " + (chalk.grey(idargs)) + " inform listener");
cb.apply(context, arguments);
}
if (next.length) {
ref1 = next.shift(), id = ref1[0], args = ref1[1], cb = ref1[2];
debug("time #" + func.__id + ": " + (chalk.grey(id)) + " restart");
args.push(cb);
return nfunc.apply(this, args);
}
});
return func.apply(context, args);
};
return nfunc;
};
module.exports.wait = function(context, func) {
var done, listeners, results, started;
if (!func) {
func = context;
context = void 0;
}
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
started = false;
done = false;
listeners = [];
results = [];
if (func.__id == null) {
func.__id = ++functionId;
}
debug("wait #" + func.__id + ": created for " + (chalk.grey(func)));
if (context) {
debug("wait #" + func.__id + ": using specific context");
}
return function() {
var args, cb, ref;
args = [].slice.call(arguments);
cb = (ref = args.pop()) != null ? ref : {};
if (done) {
debug("wait #" + func.__id + ": called again -> send result");
return cb.apply(context, results);
}
listeners.push(cb);
if (started) {
debug("wait #" + func.__id + ": called again while running");
return;
}
debug("wait #" + func.__id + ": called");
started = true;
args.push(function() {
var i, len;
debug("wait #" + func.__id + ": done");
done = true;
results = [].slice.call(arguments);
for (i = 0, len = listeners.length; i < len; i++) {
cb = listeners[i];
debug("wait #" + func.__id + ": inform listener");
cb.apply(context, results);
}
return listeners = null;
});
return func.apply(context, args);
};
};
}).call(this);
//# sourceMappingURL=once.map