diff --git a/source/Ox/js/Function.js b/source/Ox/js/Function.js index 750fd3da..d69be7f9 100644 --- a/source/Ox/js/Function.js +++ b/source/Ox/js/Function.js @@ -85,17 +85,29 @@ Ox.noop = function() { /*@ Ox.queue Queue of asynchronous function calls with cached results - The results are cached based on all but the last argument, which is the - callback. - fn function - maxThreads Number of parallel function calls - callback Callback function - result <*> Result + The results are cached based on all arguments to `fn`, except the last one, + which is the callback. + (fn, maxThreads) -> Queue function + .clear Clear method + fn Queued function + maxThreads 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; };