commit acfd317de9d31b127e42ab3ce3fded2be314baa8
parent 5d32e800fc38fc167486c0845ca6d701450a723e
Author: Simon Kornblith <simon@simonster.com>
Date: Tue, 14 Jun 2011 21:26:48 +0000
- Addresses #1828, Ensure any code that uses nsITimer directly maintains a global reference until callback executes
- Adds an optional runWhenWaiting argument to Zotero.setTimeout(), so that code can be set to run even if Zotero.wait() is in progress
Diffstat:
3 files changed, 12 insertions(+), 20 deletions(-)
diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js
@@ -78,10 +78,8 @@ Zotero.Translate.ItemSaver.prototype = {
Zotero.debug("Translate: Beginning transaction");
Zotero.DB.beginTransaction();
- this._timer = Components.classes["@mozilla.org/timer;1"].
- createInstance(Components.interfaces.nsITimer);
- this._timer.initWithCallback(this, 0, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
-
+ var me = this;
+ Zotero.setTimeout(function() { me._closeTransaction() }, 0);
Zotero.showZoteroPaneProgressMeter(Zotero.getString("ingester.scraping"), false);
}
@@ -514,15 +512,9 @@ Zotero.Translate.ItemSaver.prototype = {
},
/**
- * Implements nsITimer.notify, closing the transaction when the current code block finishes
- * executing.
+ * Closes the transaction when the current code block finishes executing.
*/
- "notify":function() {
- if(Zotero.waiting) {
- this._timer.initWithCallback(this, 0, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
- return;
- }
-
+ "_closeTransaction":function() {
Zotero.debug("Translate: Closing transaction");
Zotero.hideZoteroPaneOverlay();
Zotero.DB.commitTransaction();
diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js
@@ -1523,17 +1523,19 @@ if(appInfo.platformVersion[0] >= 2) {
};
/**
- * Emulates the behavior of window.setTimeout, but ensures that timeouts do not get called
+ * Emulates the behavior of window.setTimeout, but ensures that callbacks 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
+ * @param {Function} func The function to be called
+ * @param {Integer} ms The number of milliseconds to wait before calling func
+ * @param {Boolean} runWhenWaiting True if the callback should be run even if Zotero.wait()
+ * is executing
*/
- this.setTimeout = function(func, ms) {
+ this.setTimeout = function(func, ms, runWhenWaiting) {
var timer = Components.classes["@mozilla.org/timer;1"].
createInstance(Components.interfaces.nsITimer);
var timerCallback = {"notify":function() {
- if(_waiting) {
+ if(_waiting && !runWhenWaiting) {
// if our callback gets called during Zotero.wait(), queue it to be set again
// when Zotero.wait() completes
_waitTimers.push(timer);
diff --git a/components/zotero-command-line-handler.js b/components/zotero-command-line-handler.js
@@ -62,9 +62,7 @@ ZoteroCommandLineHandler.prototype = {
// Not quite sure why this is necessary to get the appropriate scoping
var Zotero = this.Zotero;
- var timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
- timer.initWithCallback({notify:function() { Zotero.Integration.execCommand(agent, command, docId) }}, 0,
- Components.interfaces.nsITimer.TYPE_ONE_SHOT);
+ Zotero.setTimeout(function() { Zotero.Integration.execCommand(agent, command, docId) }, 0);
}
// handler for Windows IPC commands