Created
July 30, 2012 13:07
-
-
Save skrat/3206758 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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