From adea34c84e4ae8ec952e335a83562c11950be4ba Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 1 Dec 2013 13:12:07 +0100 Subject: [PATCH] Ox.cache: extend options only once --- source/Ox/js/Function.js | 58 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/source/Ox/js/Function.js b/source/Ox/js/Function.js index dadda6df..d023f0ea 100644 --- a/source/Ox/js/Function.js +++ b/source/Ox/js/Function.js @@ -16,36 +16,36 @@ Ox.cache Memoize a function @*/ // TODO: add async test Ox.cache = function(fn, options) { - var cache = {}, - ret = function() { - options = Ox.extend({ - async: false, - key: JSON.stringify - }, options || {}); - var args = Ox.toArray(arguments), key = options.key(args); - function callback() { - // cache all arguments passed to callback - cache[key] = Ox.toArray(arguments); - // call the original callback - Ox.last(args).apply(this, arguments); - } - if (options.async) { - if (!(key in cache)) { - // call function with patched callback - fn.apply(this, args.slice(0, -1).concat(callback)); - } else { - // call callback with cached arguments - setTimeout(function() { - callback.apply(this, cache[key]); - }); - } + var cache = {}, ret; + options = Ox.extend({ + async: false, + key: JSON.stringify + }, options || {}); + ret = function() { + var args = Ox.slice(arguments), key = options.key(args); + function callback() { + // cache all arguments passed to callback + cache[key] = Ox.slice(arguments); + // call the original callback + Ox.last(args).apply(this, arguments); + } + if (options.async) { + if (!(key in cache)) { + // call function with patched callback + fn.apply(this, args.slice(0, -1).concat(callback)); } else { - if (!(key in cache)) { - cache[key] = fn.apply(this, args); - } - return cache[key]; + // call callback with cached arguments + setTimeout(function() { + callback.apply(this, cache[key]); + }); } - }; + } else { + if (!(key in cache)) { + cache[key] = fn.apply(this, args); + } + return cache[key]; + } + }; ret.clear = function() { if (arguments.length == 0) { cache = {}; @@ -99,7 +99,7 @@ Ox.queue = function(fn, maxThreads) { processing = [], queued = [], ret = Ox.cache(function() { - var args = Ox.toArray(arguments); + var args = Ox.slice(arguments); queued.push({args: args, key: getKey(args)}); process(); }, {async: true, key: getKey}),