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.
This commit is contained in:
parent
21aff6f467
commit
4607239fa9
|
@ -1509,6 +1509,26 @@ if(appInfo.platformVersion[0] >= 2) {
|
||||||
window.zoteroLastRepaint = now;
|
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
|
* Emulates the behavior of window.setTimeout, but ensures that callbacks do not get called
|
||||||
* during Zotero.wait()
|
* during Zotero.wait()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user