commit 3f69b8b9c09ceb9502f7ea766c2fda9b17805c9d
parent 58a3680ca5a8b8900d3c2158037afe0ad4511bd2
Author: Simon Kornblith <simon@simonster.com>
Date: Tue, 31 May 2011 19:59:07 +0000
Don't allow functions queued with setTimeout() to execute during Zotero.wait()
Fixes fatal error during import
Diffstat:
2 files changed, 36 insertions(+), 8 deletions(-)
diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js
@@ -176,6 +176,11 @@ var Zotero = new function(){
var _progressMeters;
var _lastPercentage;
+ /**
+ * A set of nsITimerCallbacks to be executed when Zotero.wait() completes
+ */
+ var _waitTimerCallbacks = [];
+
/*
* Initialize the extension
*/
@@ -1365,10 +1370,39 @@ var Zotero = new function(){
_waiting = false;
+ // requeue nsITimerCallbacks that came up during Zotero.wait() but couldn't execute
+ for each(var timerCallback in _waitTimerCallbacks) {
+ var timer = Components.classes["@mozilla.org/timer;1"].
+ createInstance(Components.interfaces.nsITimer);
+ timer.initWithCallback(timerCallback, 0, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
+ }
+
//Zotero.debug("Waited " + cycles + " cycles");
return;
};
+ /**
+ * Emulates the behavior of window.setTimeout, but ensures that timeouts do not get called
+ * during Zotero.wait()
+ *
+ * @param {Function} func The function to be called
+ * @param {Integer} ms The number of milliseconds to wait before calling func
+ */
+ this.setTimeout = function(func, ms) {
+ var timer = Components.classes["@mozilla.org/timer;1"].
+ createInstance(Components.interfaces.nsITimer);
+ var timerCallback = {"notify":function() {
+ if(_waiting) {
+ // if our callback gets called during Zotero.wait(), queue it to be set again
+ // when Zotero.wait() complets
+ _waitTimerCallbacks.push(timerCallback);
+ } else {
+ // otherwise, execute callback function
+ func();
+ }
+ }}
+ timer.initWithCallback(timerCallback, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
+ }
/**
* Show Zotero pane overlay and progress bar in all windows
diff --git a/components/zotero-service.js b/components/zotero-service.js
@@ -214,14 +214,8 @@ function confirm(msg){
/**
* Convenience method to replicate window.setTimeout()
**/
-// TODO: is this still used? if so, move to zotero.js
-function setTimeout(func, ms){
- var timer = Components.classes["@mozilla.org/timer;1"].
- createInstance(Components.interfaces.nsITimer);
- // {} implements nsITimerCallback
- timer.initWithCallback({notify:func}, ms,
- Components.interfaces.nsITimer.TYPE_ONE_SHOT);
- return timer;
+function setTimeout(func, ms) {
+ Zotero.setTimeout(func, ms);
}