From 4607239fa94897be45a824f66243d2da96243bce Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Wed, 21 Sep 2011 00:32:25 +0000 Subject: [PATCH] Add a function to pump a generator until it yields false, allowing other events to be processed in between. This is useful because we can then call yield true; in place of Zotero.wait() to allow UI events to be processed without exiting the function, thus avoiding the hassle of setting up a large number of callbacks. This is still painful compared to Zotero.wait(), since the yield has to be present in the generator passed to Zotero.pumpGenerator() and not a child function. However, it's less painful than using a bunch of nested setTimeout() calls. --- chrome/content/zotero/xpcom/zotero.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index ee2b953824..a6eb924c69 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -1509,6 +1509,26 @@ if(appInfo.platformVersion[0] >= 2) { window.zoteroLastRepaint = now; }; + /** + * Pumps a generator until it yields false. See schema.js for an example. + */ + this.pumpGenerator = function(generator) { + _waiting++; + + var timer = Components.classes["@mozilla.org/timer;1"]. + createInstance(Components.interfaces.nsITimer); + var timerCallback = {"notify":function() { + if(!generator.next()) { + timer.cancel(); + _runningTimers.splice(_runningTimers.indexOf(timer), 1); + _waiting--; + } + }} + timer.initWithCallback(timerCallback, ms, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK); + // add timer to global scope so that it doesn't get garbage collected before it completes + _runningTimers.push(timer); + } + /** * Emulates the behavior of window.setTimeout, but ensures that callbacks do not get called * during Zotero.wait()