For my angular application, i'm trying to track how many promises have been processed and how many still need processing.
My code decorates $q with wrapper methods that update a simple counter whenever an operation is started and whenever one is finished, seems simple:
angular.module('DuckieTV',[])
.config(function($provide) {
var count = window.promiseStats = {
open: 0,
done: 0
};
$provide.decorator('$q', function($delegate) {
function decorate(p) {
p._then = p.then;
p.then = function(thenFn, errFn, notifyFn) {
count.open++;
return p._then(function() {
count.done++;
if(thenFn) return thenFn.apply(this,arguments)
}, function() {
count.done++;
if(errFn) return errFn.apply(this,arguments)
}, notifyFn);
};
p._finally = p.finally;
p.finally = function(callback) {
count.done++;
p._finally(callback)
}
p._catch = p.catch;
p.catch = function(callback) {
count.done++;
p._catch(callback)
}
return p;
}
var d = $delegate.defer;
$delegate.defer = function() {
var deferred = d();
decorate(deferred.promise);
return deferred;
};
return $delegate;
});
})
The fun begins when I'm noticing a discrepancy between started/finished promises. After just a few minutes of performing lots of deferred operations it can become a > 15% percentage overall.
Example console output:
promiseStats
Object {open: 99, done: 95}
Math.floor(promiseStats.done / promiseStats.open * 100);
94
after some work (import operations)
promiseStats;
Object {open: 185, done: 172}
Math.floor(promiseStats.done / promiseStats.open * 100);
92
My actual question: Can anyone tell me if i'm missing something with this implementation?
As far as I know, I haven't missed optional implementations and all the promises that i'm firing via .then are properly coded