summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaolan McMahon <caolan@caolanmcmahon.com>2011-10-14 10:01:32 -0700
committerCaolan McMahon <caolan@caolanmcmahon.com>2011-10-14 10:01:32 -0700
commit52359e5c376b97826c1efc819f9fe63ff23966a1 (patch)
treeacb116ca7abbd2d2828cadcf0155b3c343a46d69
parent7c76fc7c258d5d4e7c69d9aadd0db05225ef5956 (diff)
parent6b4b0200fa9772dd6a1232456352019f3ed937ac (diff)
downloadasync-52359e5c376b97826c1efc819f9fe63ff23966a1.tar.gz
Merge branch 'unmemoize' of https://github.com/balinterdi/async into balinterdi-unmemoize
-rw-r--r--README.md10
-rw-r--r--lib/async.js10
-rw-r--r--test/test-async.js41
3 files changed, 60 insertions, 1 deletions
diff --git a/README.md b/README.md
index 419fa07..6558bf2 100644
--- a/README.md
+++ b/README.md
@@ -96,6 +96,7 @@ So far its been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage:
### Utils
* [memoize](#memoize)
+* [unmemoize](#unmemoize)
* [log](#log)
* [dir](#dir)
* [noConflict](#noConflict)
@@ -907,6 +908,15 @@ __Example__
// callback
});
+<a name="unmemoize" />
+### unmemoize(fn)
+
+Undoes a memoized function, reverting it to the original, unmemoized
+form. Comes handy in tests.
+
+__Arguments__
+
+* fn - the memoized function
<a name="log" />
### log(function, arguments)
diff --git a/lib/async.js b/lib/async.js
index ec020cc..647a225 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -650,7 +650,7 @@
hasher = hasher || function (x) {
return x;
};
- return function () {
+ var memoized = function () {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var key = hasher.apply(null, args);
@@ -664,6 +664,14 @@
}]));
}
};
+ memoized.unmemoized = fn;
+ return memoized;
+ };
+
+ async.unmemoize = function (fn) {
+ return function () {
+ return (fn.unmemoized || fn).apply(null, arguments);
+ }
};
}());
diff --git a/test/test-async.js b/test/test-async.js
index df7d27b..3de9515 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -1334,6 +1334,47 @@ exports['memoize'] = function (test) {
test.done();
};
+exports['unmemoize'] = function(test) {
+ test.expect(4);
+ var call_order = [];
+
+ var fn = function (arg1, arg2, callback) {
+ call_order.push(['fn', arg1, arg2]);
+ callback(null, arg1 + arg2);
+ };
+
+ var fn2 = async.memoize(fn);
+ var fn3 = async.unmemoize(fn2);
+ fn3(1, 2, function (err, result) {
+ test.equal(result, 3);
+ });
+ fn3(1, 2, function (err, result) {
+ test.equal(result, 3);
+ });
+ fn3(2, 2, function (err, result) {
+ test.equal(result, 4);
+ });
+
+ test.same(call_order, [['fn',1,2], ['fn',1,2], ['fn',2,2]]);
+
+ test.done();
+}
+
+exports['unmemoize a not memoized function'] = function(test) {
+ test.expect(1);
+
+ var fn = function (arg1, arg2, callback) {
+ callback(null, arg1 + arg2);
+ };
+
+ var fn2 = async.unmemoize(fn);
+ fn2(1, 2, function(err, result) {
+ test.equal(result, 3);
+ });
+
+ test.done();
+}
+
exports['memoize error'] = function (test) {
test.expect(1);
var testerr = new Error('test');