commit 4607239fa94897be45a824f66243d2da96243bce
parent 21aff6f467623f83f1d08ba14e7a6efa2d19c80b
Author: Simon Kornblith <simon@simonster.com>
Date: Wed, 21 Sep 2011 00:32:25 +0000
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.
Diffstat:
1 file changed, 20 insertions(+), 0 deletions(-)
diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js
@@ -1510,6 +1510,26 @@ if(appInfo.platformVersion[0] >= 2) {
};
/**
+ * 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()
*