Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

promise keeper - from gist https://gist.github.com/3729142 for Q issue

  • Loading branch information...
commit 3a2a3fc79d34f6a9f01729bc8813bb0884ea1cf1 1 parent 406d3af
@dominykas dominykas authored
Showing with 103 additions and 0 deletions.
  1. +71 −0 04-promiseKeeper.js
  2. +32 −0 04-promiseKeeper.use.js
View
71 04-promiseKeeper.js
@@ -0,0 +1,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;
+};
View
32 04-promiseKeeper.use.js
@@ -0,0 +1,32 @@
+var keeper = require('./04-promiseKeeper');
+var Q = require('q');
+
+var PROBABILITY_OF_RESOLVE = 0.1;
+var N_PUSHER = 100;
+var N_RESOLVER = 200;
+var N_LOG = 500;
+
+
+var deferreds = [];
+
+var resolver = function() {
+ console.log("Resolving");
+ deferreds = deferreds.filter(function(p){
+ var shouldResolve = Math.random() <= PROBABILITY_OF_RESOLVE;
+ if (shouldResolve)
+ {
+ p.resolve();
+ }
+ return !shouldResolve;
+ });
+ setTimeout(resolver, Math.random()*N_RESOLVER);
+};
+var pusher = function() {
+ console.log("Pushing");
+ var deferred = Q.defer();
+ deferreds.push(deferred);
+ setTimeout(pusher, Math.random()*N_PUSHER);
+};
+resolver();
+pusher();
+setInterval(keeper.snapshot, N_LOG);
Please sign in to comment.
Something went wrong with that request. Please try again.