From a35461e2fc6847d09e01467bddd3fc27a8b51131 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Tue, 10 Jul 2012 15:27:38 -0400 Subject: [PATCH] Rename q.jsm to q.js, and use a version of q that's closer to the original and allows use in script tags as well --- chrome/content/zotero/xpcom/file.js | 2 +- chrome/content/zotero/xpcom/http.js | 2 +- chrome/content/zotero/xpcom/style.js | 2 +- resource/{q.jsm => q.js} | 78 ++++++++++++++++------------ 4 files changed, 48 insertions(+), 36 deletions(-) rename resource/{q.jsm => q.js} (96%) diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js index b64f73356a..245c2c484c 100644 --- a/chrome/content/zotero/xpcom/file.js +++ b/chrome/content/zotero/xpcom/file.js @@ -28,7 +28,7 @@ * @namespace */ Zotero.File = new function(){ - Components.utils.import("resource://zotero/q.jsm"); + Components.utils.import("resource://zotero/q.js"); Components.utils.import("resource://gre/modules/NetUtil.jsm"); Components.utils.import("resource://gre/modules/FileUtils.jsm"); diff --git a/chrome/content/zotero/xpcom/http.js b/chrome/content/zotero/xpcom/http.js index 82e96a1c80..26889164e9 100644 --- a/chrome/content/zotero/xpcom/http.js +++ b/chrome/content/zotero/xpcom/http.js @@ -3,7 +3,7 @@ * @namespace */ Zotero.HTTP = new function() { - Components.utils.import("resource://zotero/q.jsm"); + Components.utils.import("resource://zotero/q.js"); /** * Exception returned for unexpected status when promise* is used diff --git a/chrome/content/zotero/xpcom/style.js b/chrome/content/zotero/xpcom/style.js index bdf1672e61..2f05523fd3 100644 --- a/chrome/content/zotero/xpcom/style.js +++ b/chrome/content/zotero/xpcom/style.js @@ -32,7 +32,7 @@ Zotero.Styles = new function() { var _initialized = false; var _styles, _visibleStyles, _cacheTranslatorData; - Components.utils.import("resource://zotero/q.jsm"); + Components.utils.import("resource://zotero/q.js"); Components.utils.import("resource://gre/modules/Services.jsm"); this.xsltProcessor = null; diff --git a/resource/q.jsm b/resource/q.js similarity index 96% rename from resource/q.jsm rename to resource/q.js index c80cc24c99..aa1154b66f 100644 --- a/resource/q.jsm +++ b/resource/q.js @@ -58,39 +58,6 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -var EXPORTED_SYMBOLS = ["Q"]; -var setTimeout = new function() { - var _runningTimers = []; - return function setTimeout(func, ms) { - var timer = Components.classes["@mozilla.org/timer;1"]. - createInstance(Components.interfaces.nsITimer); - var timerCallback = {"notify":function() { - // remove timer from global scope, so it can be garbage collected - _runningTimers.splice(_runningTimers.indexOf(timer), 1); - // execute callback function - try { - func(); - } catch(err) { - var scriptError = Components.classes["@mozilla.org/scripterror;1"] - .createInstance(Components.interfaces.nsIScriptError); - scriptError.init( - err.message ? err.message : err.toString(), - err.fileName ? err.fileName : (err.filename ? err.filename : null), - null, - err.lineNumber ? err.lineNumber : null, - null, - scriptError['errorFlag'], - 'component javascript' - ); - Components.classes["@mozilla.org/consoleservice;1"] - .getService(Components.interfaces.nsIConsoleService) - .logMessage(scriptError); - } - }}; - timer.initWithCallback(timerCallback, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT); - } -}; - (function (definition) { // Turn off strict mode for this function so we can assign to global.Q /*jshint strict: false*/ @@ -118,6 +85,51 @@ var setTimeout = new function() { return definition(void 0, Q); }; } + + // Mozilla JSM + } else if (~String(this).indexOf('BackstagePass')) { + EXPORTED_SYMBOLS = ["Q"]; + + // Q expects an implementation of setTimeout + setTimeout = new function() { + // We need to maintain references to running nsITimers. Otherwise, they can + // get garbage collected before they fire. + var _runningTimers = []; + + return function setTimeout(func, ms) { + var timer = Components.classes["@mozilla.org/timer;1"]. + createInstance(Components.interfaces.nsITimer); + timer.initWithCallback({"notify":function() { + // Remove timer from array so it can be garbage collected + _runningTimers.splice(_runningTimers.indexOf(timer), 1); + + // Execute callback function + try { + func(); + } catch(err) { + // Rethrow errors that occur so that they appear in the error + // console with the appropriate name and line numbers. While the + // the errors appear without this, the line numbers get eaten. + var scriptError = Components.classes["@mozilla.org/scripterror;1"] + .createInstance(Components.interfaces.nsIScriptError); + scriptError.init( + err.message || err.toString(), + err.fileName || err.filename || null, + null, + err.lineNumber || null, + null, + scriptError.errorFlag, + 'component javascript' + ); + Components.classes["@mozilla.org/consoleservice;1"] + .getService(Components.interfaces.nsIConsoleService) + .logMessage(scriptError); + } + }}, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT); + _runningTimers.push(timer); + } + }; + definition(void 0, Q = {}); //