alinex-async
Version:
An extended version of the async library.
107 lines (102 loc) • 2.78 kB
JavaScript
(function() {
module.exports["throw"] = function(func, context) {
var called;
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
called = false;
return function() {
if (called) {
throw new Error("This function should only be called once.");
}
called = true;
return func.apply(context, arguments);
};
};
module.exports.skip = function(func, context) {
var called;
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
called = false;
return function() {
var cb, err;
cb = arguments[arguments.length - 1];
if (called) {
err = new Error("This function should only be called once.");
if (typeof cb === 'function') {
return cb(err);
}
return err;
}
called = true;
return func.apply(context, arguments);
};
};
module.exports.time = function(func, context) {
var listeners, started;
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
started = false;
listeners = [];
return function() {
var args, cb, ref;
args = [].slice.call(arguments);
cb = (ref = args.pop()) != null ? ref : {};
listeners.push(cb);
if (started) {
return;
}
started = true;
args.push(function() {
var i, len, results1, work;
started = false;
work = [].slice.call(listeners);
listeners = [];
results1 = [];
for (i = 0, len = work.length; i < len; i++) {
cb = work[i];
results1.push(cb.apply(null, arguments));
}
return results1;
});
return func.apply(context, args);
};
};
module.exports.wait = function(func, context) {
var done, listeners, results, started;
if (typeof func !== 'function') {
throw new Error("Argument func is not a function!");
}
started = false;
done = false;
listeners = [];
results = [];
return function() {
var args, cb, ref;
args = [].slice.call(arguments);
cb = (ref = args.pop()) != null ? ref : {};
if (done) {
return cb.apply(null, results);
}
listeners.push(cb);
if (started) {
return;
}
started = true;
args.push(function() {
var i, len;
done = true;
results = [].slice.call(arguments);
for (i = 0, len = listeners.length; i < len; i++) {
cb = listeners[i];
cb.apply(null, results);
}
return listeners = null;
});
return func.apply(context, args);
};
};
}).call(this);
//# sourceMappingURL=once.map