oxjs/source/Ox/js/Function.js
2012-05-19 12:09:48 +04:00

66 lines
No EOL
1.9 KiB
JavaScript

'use strict';
/*@
Ox.cache <f> Memoize a function
<script>
Ox.test.fn = Ox.cache(function(n) { return n * Math.random(); });
</script>
> Ox.test.fn(10) == Ox.test.fn(10);
true
> Ox.test.fn(10) == Ox.test.fn.clear()(10);
false
@*/
Ox.cache = function(fn, options) {
options = Ox.extend({
async: false,
key: JSON.stringify
}, options || {})
var cache = {},
ret = function() {
var args = Ox.makeArray(arguments),
callback,
key = options.key(args);
function callback() {
// cache all arguments passed to callback
cache[key] = Ox.makeArray(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, Ox.merge(Ox.sub(args, 0, -1), callback));
} else {
// call callback with cached arguments
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 = {};
} else {
Ox.toArray(arguments).forEach(function(key) {
delete cache[key];
});
}
return ret;
}
return ret;
};
Ox.identity = function(val) {
return val;
};
/*@
Ox.void <f> Returns nothing and calls options callback without arguments
This can be useful to combine a synchronous and an asynchronous code path.
@*/
Ox.void = function(callback) {
Ox.isFunction(callback) && callback();
};