cleanup Ox.queue
This commit is contained in:
parent
f938f281f1
commit
b0f9558845
2 changed files with 37 additions and 40 deletions
|
|
@ -82,3 +82,40 @@ Ox.noop = function() {
|
|||
var callback = Ox.last(arguments);
|
||||
Ox.isFunction(callback) && callback();
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.queue <f> Queue of asynchronous function calls with cached results
|
||||
The results are cached based on all but the last argument, which is the
|
||||
callback.
|
||||
fn <f> function
|
||||
maxThreads <n> Number of parallel function calls
|
||||
callback <f> Callback function
|
||||
result <*> Result
|
||||
@*/
|
||||
Ox.queue = function(fn, maxThreads) {
|
||||
var maxThreads = maxThreads || 10,
|
||||
queue = [],
|
||||
threads = 0;
|
||||
function process() {
|
||||
var n = Math.min(queue.length, maxThreads - threads);
|
||||
if (n) {
|
||||
threads += n;
|
||||
Ox.parallelForEach(queue.splice(0, n), function(args, index, array, callback) {
|
||||
fn.apply(this, args.slice(0, -1).concat(function(result) {
|
||||
threads--;
|
||||
args.slice(-1)[0](result);
|
||||
callback();
|
||||
}));
|
||||
}, process);
|
||||
}
|
||||
}
|
||||
return Ox.cache(function() {
|
||||
queue.push(Ox.toArray(arguments));
|
||||
process();
|
||||
}, {
|
||||
async: true,
|
||||
key: function(args) {
|
||||
return JSON.stringify(args.slice(0, -1));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue