Permalink
Browse files

Add itemCount getter property

Fix #5
  • Loading branch information...
isaacs committed Jan 15, 2013
1 parent 1d7634d commit d68cdd5d8e220743262c19afe30fdba7a2b30cca
Showing with 16 additions and 0 deletions.
  1. +8 −0 ac.js
  2. +8 −0 test/basic.js
View
8 ac.js
@@ -19,6 +19,14 @@ function AsyncCache(opt) {
this._allowStale = opt.stale;
}
+Object.defineProperty(AsyncCache.prototype, 'itemCount', {
+ get: function() {
+ return this._cache.itemCount;
+ },
+ enumerable: true,
+ configurable: true
+});
+
AsyncCache.prototype.get = function(key, cb) {
if (this._loading[key])
return this._loading[key].push(cb);
View
@@ -14,9 +14,11 @@ test('basic', function(t) {
var stFirst = null;
var stSecond = null;
+ t.equal(ac.itemCount, 0);
ac.get(__filename, afterFirst);
function afterFirst(er, st) {
if (er) throw er;
+ t.equal(ac.itemCount, 1);
called ++;
stFirst = st;
t.pass('called the first one');
@@ -30,6 +32,7 @@ test('basic', function(t) {
ac.get(__filename, afterSecond);
function afterSecond(er, st) {
if (er) throw er;
+ t.equal(ac.itemCount, 1);
called ++;
stSecond = st;
t.pass('called the second one');
@@ -40,6 +43,7 @@ test('basic', function(t) {
t.deepEqual(ac._loading, expectLoading);
function next() {
+ t.equal(ac.itemCount, 1);
t.equal(stFirst, stSecond, 'should be same stat object');
t.deepEqual(ac._loading, {});
t.equal(called, 2);
@@ -54,8 +58,10 @@ test('basic', function(t) {
// now make it fall out of cache by fetching a new one.
ac.get(__dirname, function(er, st) {
if (er) throw er;
+ t.equal(ac.itemCount, 1);
ac.get(__filename, function(er, st) {
if (er) throw er;
+ t.equal(ac.itemCount, 1);
t.notEqual(st, stFirst, 'should have re-fetched');
t.end();
});
@@ -76,7 +82,9 @@ test('allow stale', function(t) {
stale: true
});
+ t.equal(ac.itemCount, 0);
ac.get('foo', function(er, val) {
+ t.equal(ac.itemCount, 1);
console.error('result', er, val);
t.equal(val, 0);
var start = Date.now();

0 comments on commit d68cdd5

Please sign in to comment.