Comparing version 1.5.0 to 1.6.0
@@ -16,5 +16,7 @@ | ||
var deferred = Fireproof._checkQ().defer(); | ||
var self = this; | ||
var deferred = Fireproof._checkQ().defer(), | ||
self = this; | ||
Fireproof.stats._addListener(self.toString()); | ||
self._ref.transaction(updateFunction, function(err, committed, snap) { | ||
@@ -65,4 +67,7 @@ | ||
var deferred = Fireproof._checkQ().defer(), | ||
resolved = false; | ||
resolved = false, | ||
self = this; | ||
Fireproof.stats._addListener(self.toString()); | ||
if (typeof callback !== 'function') { | ||
@@ -78,3 +83,3 @@ callback = function() {}; | ||
Fireproof.stats._record('read', this.toString()); | ||
Fireproof.stats._record('read', self.toString()); | ||
snap = new Fireproof.Snapshot(snap); | ||
@@ -92,8 +97,10 @@ | ||
}.bind(this); | ||
}; | ||
callbackHandler.then = deferred.promise.then.bind(deferred.promise); | ||
this._ref.on(eventType, callbackHandler, function(err) { | ||
self._ref.on(eventType, callbackHandler, function(err) { | ||
Fireproof.stats._removeListener(self.toString()); | ||
Fireproof._nextTick(function() { | ||
@@ -124,3 +131,6 @@ cancelCallback(err); | ||
Fireproof.prototype.off = function(eventType, callback, context) { | ||
Fireproof.stats._removeListener(this.toString()); | ||
this._ref.off(eventType, callback, context); | ||
}; | ||
@@ -140,4 +150,7 @@ | ||
var deferred = Fireproof._checkQ().defer(); | ||
var deferred = Fireproof._checkQ().defer(), | ||
self = this; | ||
Fireproof.stats._addListener(self.toString()); | ||
if (typeof successCallback !== 'function') { | ||
@@ -151,6 +164,5 @@ successCallback = function() {}; | ||
var self = this; | ||
self._ref.once(eventType, function(snap) { | ||
Fireproof.stats._removeListener(self.toString()); | ||
Fireproof.stats._record('read', self.toString()); | ||
@@ -166,2 +178,3 @@ snap = new Fireproof.Snapshot(snap); | ||
Fireproof.stats._removeListener(self.toString()); | ||
deferred.reject(err); | ||
@@ -168,0 +181,0 @@ Fireproof._nextTick(function() { |
@@ -11,2 +11,3 @@ | ||
* @method Fireproof.stats.reset | ||
* @method Fireproof.stats.reset | ||
*/ | ||
@@ -23,2 +24,4 @@ Fireproof.stats.reset = function() { | ||
Fireproof.stats.listeners = {}; | ||
}; | ||
@@ -34,7 +37,76 @@ | ||
Fireproof.stats._record = function(name, path) { | ||
console.log('heard', name, 'from', path); | ||
Fireproof.stats.events[name].push({ path: path, time: Date.now() }); | ||
}; | ||
/** | ||
* Adds a Firebase listener. | ||
* @private | ||
* @method Fireproof.stats._addListener | ||
* @param {String} path The Firebase path of the listener. | ||
*/ | ||
Fireproof.stats._addListener = function(path) { | ||
if (!Fireproof.stats.listeners[path]) { | ||
Fireproof.stats.listeners[path] = 1; | ||
} else { | ||
Fireproof.stats.listeners[path]++; | ||
} | ||
}; | ||
/** | ||
* Removes a Firebase listener. | ||
* @private | ||
* @method Fireproof.stats._removeListener | ||
* @param {String} path The Firebase path of the listener. | ||
*/ | ||
Fireproof.stats._removeListener = function(path) { | ||
if (Fireproof.stats.listeners[path] === 1) { | ||
delete Fireproof.stats.listeners[path]; | ||
} else if (Fireproof.stats.listeners[path]) { | ||
Fireproof.stats.listeners[path]--; | ||
} | ||
}; | ||
/** | ||
* Gets data about listeners on Firebase locations. | ||
* @method Fireproof.stats.getListeners | ||
* @returns {Object} Listener counts keyed by Firebase path. | ||
*/ | ||
Fireproof.stats.getListeners = function() { | ||
return Object.keys(Fireproof.stats.listeners) | ||
.reduce(function(acc, k) { | ||
acc[k] = Fireproof.stats.listeners[k]; | ||
return acc; | ||
}, {}); | ||
}; | ||
/** | ||
* Gets the total number of listeners on Firebase locations. | ||
* @method Fireproof.stats.getListenerCount | ||
* @returns {Number} The total number of Firebase listeners presently operating. | ||
*/ | ||
Fireproof.stats.getListenerCount = function() { | ||
return Object.keys(Fireproof.stats.listeners) | ||
.reduce(function(acc, k) { | ||
return acc + Fireproof.stats.listeners[k]; | ||
}, 0); | ||
}; | ||
/** | ||
* Gets the per-operation, per-path counts of Firebase operations. | ||
* @method Fireproof.stats.getPathCounts | ||
* @returns {Object} An object with three keys: "read", "write", | ||
@@ -70,3 +142,4 @@ * and "update". Each key has an object value, of which the keys are Firebase | ||
* Gets the per-operation counts of Firebase operations. | ||
* @returns {Object} An object with four keys: "read", "write", and | ||
* @methods Fireproof.stats.getCounts | ||
* @returns {Object} An object with three keys: "read", "write", and | ||
* "update". The values are the counts of operations under those headings. | ||
@@ -86,4 +159,5 @@ */ | ||
Fireproof.stats.reset(); | ||
}; |
{ | ||
"name": "fireproof", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Promises for Firebase objects.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,4 @@ | ||
this.timeout(5000); | ||
fireproof = new Fireproof(firebase); | ||
@@ -22,0 +24,0 @@ |
@@ -37,2 +37,4 @@ | ||
this.timeout(5000); | ||
return Q.all([ | ||
@@ -42,10 +44,16 @@ statsRef.child('a').set(true), | ||
statsRef.child('b2').remove(), | ||
statsRef.child('c').update({ foo: 'bar '}) | ||
]) | ||
.then(function() { | ||
return statsRef.child('a').once('value', function() {}); | ||
}); | ||
statsRef.child('c').update({ foo: 'bar '}), | ||
statsRef.child('x').on('value', function() {}).then(function() {}), | ||
statsRef.child('y').on('value', function() {}).then(function() {}), | ||
statsRef.child('z').on('value', function() {}).then(function() {}) | ||
]); | ||
}); | ||
after(function() { | ||
statsRef.child('a').off('value'); | ||
statsRef.child('b').off('value'); | ||
statsRef.child('c').off('value'); | ||
}); | ||
describe('#getCounts', function() { | ||
@@ -57,3 +65,3 @@ | ||
expect(stats).to.deep.equal({ | ||
read: 1, | ||
read: 3, | ||
write: 3, | ||
@@ -86,6 +94,11 @@ update: 1 | ||
console.log(pathStats); | ||
console.log(correctedPathStats); | ||
expect(correctedPathStats).to.deep.equal({ | ||
read: { | ||
'/statsRef/a': 1 | ||
'/statsRef/x': 1, | ||
'/statsRef/y': 1, | ||
'/statsRef/z': 1 | ||
}, | ||
@@ -107,4 +120,35 @@ write: { | ||
describe('#getListeners', function() { | ||
it('returns an object counting the current listeners by path', function() { | ||
var listeners = Fireproof.stats.getListeners(); | ||
var correctedPathListeners = Object.keys(listeners) | ||
.reduce(function(acc, path) { | ||
acc[url.parse(path).pathname] = listeners[path]; | ||
return acc; | ||
}, {}); | ||
expect(correctedPathListeners).to.deep.equal({ | ||
'/statsRef/x': 1, | ||
'/statsRef/y': 1, | ||
'/statsRef/z': 1 | ||
}); | ||
}); | ||
}); | ||
describe('#getListenerCount', function() { | ||
it('returns the total number of current listeners', function() { | ||
expect(Fireproof.stats.getListenerCount()).to.equal(3); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51763
1445