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

This commit is contained in:
Simon Kornblith 2012-07-10 15:27:38 -04:00
parent 06825c4767
commit a35461e2fc
4 changed files with 48 additions and 36 deletions

View file

@ -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");

View file

@ -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

View file

@ -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;

View file

@ -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 = {});
// <script>
} else {