[Pkg-javascript-commits] [node-async] 83/480: memoize - handle multiple calls before completion
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:14 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository node-async.
commit c28f0b806b31b85270cf842f020f27480f2efc37
Author: braddunbar <dunbarb2 at gmail.com>
Date: Fri May 13 12:13:27 2011 -0400
memoize - handle multiple calls before completion
---
lib/async.js | 11 ++++++++++-
test/test-async.js | 18 ++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/lib/async.js b/lib/async.js
index 8c863ac..e83581e 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -610,6 +610,7 @@
async.memoize = function (fn, hasher) {
var memo = {};
+ var queues = {};
hasher = hasher || function (x) {
return x;
};
@@ -620,10 +621,18 @@
if (key in memo) {
callback.apply(null, memo[key]);
}
+ else if (key in queues) {
+ queues[key].push(callback);
+ }
else {
+ queues[key] = [callback];
fn.apply(null, args.concat([function () {
memo[key] = arguments;
- callback.apply(null, arguments);
+ var q = queues[key];
+ delete queues[key];
+ for (var i = 0, l = q.length; i < l; i++) {
+ q[i].apply(null, arguments);
+ }
}]));
}
};
diff --git a/test/test-async.js b/test/test-async.js
index 8c2cebd..17166a2 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -1231,6 +1231,24 @@ exports['memoize error'] = function (test) {
test.done();
};
+exports['memoize multiple calls'] = function (test) {
+ test.expect(3);
+ var fn = function (arg1, arg2, callback) {
+ test.ok(true);
+ setTimeout(function(){
+ callback(null, arg1, arg2);
+ }, 10);
+ };
+ var fn2 = async.memoize(fn);
+ fn2(1, 2, function(err, result) {
+ test.equal(result, 1, 2);
+ });
+ fn2(1, 2, function(err, result) {
+ test.equal(result, 1, 2);
+ test.done();
+ });
+};
+
exports['memoize custom hash function'] = function (test) {
test.expect(2);
var testerr = new Error('test');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git
More information about the Pkg-javascript-commits
mailing list