Permalink
Browse files

Fix for getting result as callback

  • Loading branch information...
1 parent 7f0a5e2 commit dbd4b3360db664de2579fb8fd1f37315ed53e0f8 @pescuma committed May 10, 2012
Showing with 43 additions and 12 deletions.
  1. +15 −8 lib/cache.js
  2. +28 −4 test/tests.js
View
@@ -120,16 +120,13 @@ function create() {
var date = now();
var result = getData(args.id);
- if (isValid(result, date)) {
- result.lastTouch = date;
-
- if (result.args[0])
+ if (args.callback) {
+ if (isValid(result, date)) {
+ result.lastTouch = date;
+ args.callback.apply(null, result.args);
return undefined;
- else
- return result.args[1];
- }
+ }
- if (args.callback) {
if (result.fetching) {
result.callbacks.push(args.callback);
@@ -152,6 +149,16 @@ function create() {
self.fetcher.apply(null, args.params);
}
+
+ } else {
+ if (isValid(result, date)) {
+ result.lastTouch = date;
+
+ if (result.args[0])
+ return undefined;
+ else
+ return result.args[1];
+ }
}
return undefined;
View
@@ -54,7 +54,8 @@ exports['Put/get tests'] = {
setTimeout(function() {
test.strictEqual(c.get(1), undefined);
-
+
+ test.expect(3);
test.done();
}, 30);
},
@@ -111,16 +112,34 @@ exports['Fetch with callback tests'] = {
callback();
},
+ 'Simple get' : function(test) {
+ c = cache.create(function(id, callback) {
+ callback(undefined, 'A');
+ });
+
+ c.get(1, function(err, data) {
+ test.equal(data, 'A');
+ });
+
+ test.expect(1);
+ test.done();
+ },
+
'Simple cache' : function(test) {
c = cache.create(function(id, callback) {
callback(undefined, 'A');
});
c.get(1, function(err, data) {
test.equal(data, 'A');
+ });
- test.done();
+ c.get(1, function(err, data) {
+ test.equal(data, 'A');
});
+
+ test.expect(2);
+ test.done();
},
'Multiple values' : function(test) {
@@ -154,6 +173,7 @@ exports['Fetch with callback tests'] = {
test.equal(calls, 2);
+ test.expect(8);
test.done();
},
@@ -166,9 +186,10 @@ exports['Fetch with callback tests'] = {
c.get(1, function(err, data) {
test.equal(c.get(1), 'A');
-
- test.done();
});
+
+ test.expect(2);
+ test.done();
},
'Cache with timer' : function(test) {
@@ -226,6 +247,7 @@ exports['Fetch with callback tests'] = {
test.equal(calls, 1);
test.equal(notifies, 3);
+ test.expect(8);
test.done();
},
@@ -353,6 +375,7 @@ exports['Function wrapper'] = {
test.strictEqual(c(1, 2, 3), 'A');
+ test.expect(7);
test.done();
},
@@ -377,6 +400,7 @@ exports['Function wrapper'] = {
setTimeout(function() {
test.strictEqual(c(1, 2, 3), undefined);
+ test.expect(8);
test.done();
}, 50);
},

0 comments on commit dbd4b33

Please sign in to comment.