Ox.queue: add clear method, cleanup documentation

This commit is contained in:
rolux 2013-10-29 14:34:15 +01:00
parent 19ae3c591a
commit 2fc8c3094d

View file

@ -85,17 +85,29 @@ Ox.noop = function() {
/*@
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
The results are cached based on all arguments to `fn`, except the last one,
which is the callback.
(fn, maxThreads) -> <f> Queue function
.clear <f> Clear method
fn <f> Queued function
maxThreads <n|10> Number of parallel function calls
@*/
Ox.queue = function(fn, maxThreads) {
var maxThreads = maxThreads || 10,
queue = [],
ret = Ox.cache(function() {
queue.push(Ox.toArray(arguments));
process();
}, {
async: true,
key: function(args) {
return JSON.stringify(args.slice(0, -1));
}
}),
threads = 0;
ret.clear = function() {
queue = [];
};
function process() {
var n = Math.min(queue.length, maxThreads - threads);
if (n) {
@ -109,13 +121,5 @@ Ox.queue = function(fn, maxThreads) {
}, process);
}
}
return Ox.cache(function() {
queue.push(Ox.toArray(arguments));
process();
}, {
async: true,
key: function(args) {
return JSON.stringify(args.slice(0, -1));
}
});
return ret;
};