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