diff options
| author | Balint Erdi <balint@secretsaucepartners.com> | 2011-10-06 19:16:50 +0200 |
|---|---|---|
| committer | Balint Erdi <balint@secretsaucepartners.com> | 2011-10-06 19:18:20 +0200 |
| commit | 6b4b0200fa9772dd6a1232456352019f3ed937ac (patch) | |
| tree | 665c7d4e579bdf27a1d85de2fe0552d3ef91e439 | |
| parent | f38b89a552ec6f914bd91dd530f6d9607a863765 (diff) | |
| download | async-6b4b0200fa9772dd6a1232456352019f3ed937ac.tar.gz | |
Unmemoize can be called with a not memoized function, too.
| -rw-r--r-- | lib/async.js | 6 | ||||
| -rw-r--r-- | test/test-async.js | 60 |
2 files changed, 41 insertions, 25 deletions
diff --git a/lib/async.js b/lib/async.js index 3b202b6..6231ec1 100644 --- a/lib/async.js +++ b/lib/async.js @@ -628,14 +628,14 @@ }])); } }; - memoized.original = fn; + memoized.unmemoized = fn; return memoized; }; async.unmemoize = function (fn) { return function () { - return fn.original.apply(null, arguments); + return (fn.unmemoized || fn).apply(null, arguments); } - } + }; }()); diff --git a/test/test-async.js b/test/test-async.js index 4c8e420..ef7a546 100644 --- a/test/test-async.js +++ b/test/test-async.js @@ -1254,28 +1254,44 @@ exports['memoize'] = function (test) { }; 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(); + 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) { |
