add Ox.queue
This commit is contained in:
parent
675e45df18
commit
c7eaee0794
1 changed files with 40 additions and 0 deletions
|
@ -284,4 +284,44 @@
|
|||
};
|
||||
// FIXME: The above test with 10000 iterations blows the stack
|
||||
|
||||
|
||||
/*@
|
||||
Ox.queue <f>
|
||||
cached process queue, passed function is exectued in parallel and
|
||||
results are cached based on all but last argument,
|
||||
last argument is a callback that gets called with results
|
||||
fn <f> function
|
||||
options <o>
|
||||
maxThreads <n> number of concurrent threads
|
||||
callback <f> gets called with object containing duration, width, height
|
||||
@*/
|
||||
Ox.queue = function(fn, options) {
|
||||
var queue = [],
|
||||
threads = 0;
|
||||
|
||||
function process() {
|
||||
var next = Math.min(queue.length, options.maxThreads-threads);
|
||||
if (next) {
|
||||
threads += next;
|
||||
Ox.parallelForEach(queue.splice(0, next), function(args, index, array, done) {
|
||||
fn.apply(this, args.slice(0, -1).concat(function(result) {
|
||||
threads--;
|
||||
args.slice(-1)[0](result);
|
||||
done();
|
||||
}));
|
||||
}, process);
|
||||
}
|
||||
}
|
||||
|
||||
return Ox.cache(function() {
|
||||
queue.push(Ox.toArray(arguments));
|
||||
process();
|
||||
}, {
|
||||
async: true,
|
||||
key: function(args) {
|
||||
return JSON.stringify(args.slice(0, -1));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
}());
|
||||
|
|
Loading…
Reference in a new issue