public
Last active

Q promise tracker

  • Download Gist
promiseKeeper.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
var noop = function(){};
// remove existing Q (hopefully it has no state...)
delete require.cache[require.resolve('q')];
 
// remove freezing so we can modify Q
var origFreeze = Object.freeze;
Object.freeze = noop;
 
// require Q and restore freezing
var Q = require('q');
Object.freeze = origFreeze;
 
// copy/pasted from Q - we'll defend it after we modify it
var defend = Object.freeze || noop;
if (typeof cajaVM !== "undefined") {
defend = cajaVM.def;
};
 
var current = [], prev = [], total=0;
 
// add the promise to an array
// make sure to remove it when it is done
// oh, and "defend" it...
var recordPromise = function(promise)
{
current.push(promise);
total++;
 
var rm = function()
{
// not the most efficient way... but who cares
current = current.filter(function(i){ return i!=promise; });
};
var nextPromise = promise.then(rm, rm);
defend(nextPromise);
return nextPromise;
}
 
 
// modify makePromise and defer - I think these are the only places
// that create promises
var origMake = Q.makePromise;
Q.makePromise = function()
{
return recordPromise(origMake.apply(this, arguments));
};
 
var origDefer = Q.defer;
Q.defer = function()
{
var deferred = origDefer.apply(this, arguments);
deferred.promise = recordPromise(deferred.promise);
return deferred;
};
 
// defend things that should've been defended...
defend(Q.makePromise.prototype);
defend(Q);
 
// logs:
// * how many are still unresolved
// * how many of those were in the previous snapshot and are still unresolved
// * how many new promises were created since last snapshot
exports.snapshot = function() {
var now = new Date().getTime();
var stillCount = current.filter(function(p){ return prev.indexOf(p)>=0; }).length;
console.log("Not kept:",current.length,"From before:",stillCount,"Generated:",total);
prev = current.slice();
total = 0;
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.