Skip to content

Instantly share code, notes, and snippets.

@skrat
Created July 30, 2012 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrat/3206758 to your computer and use it in GitHub Desktop.
Save skrat/3206758 to your computer and use it in GitHub Desktop.
function(func, hasher) {
var cache = {},
queue = {};
hasher = hasher || JSON.stringify;
return function() {
var args = Array.prototype.splice.call(arguments, 0);
var callback = args.pop();
var hash = hasher(args);
if (hash in cache) {
callback.apply(this, cache[hash]);
} else if (hash in queue) {
queue[hash].push(callback);
} else {
queue[hash] = [callback];
args.push(function() {
cache[hash] = Array.prototype.splice.call(arguments, 0);
var q = queue[hash];
delete queue[hash];
q.forEach(function(qcallback) {
qcallback.apply(null, cache[hash]);
});
});
func.apply(this, args);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment