Fix race condition when starting in Connector mode

When starting in Connector mode (i.e. Standalone is open), Zotero first starts in Full mode, looks for Standalone, then "shuts down" and restarts in Connector mode. `Zotero.shutdown()` returns a promise which is then followed up by a `Zotero.init` call. Thus, when starting in Connector mode, Zotero initialization is asynchronous and makes it possible for `Zotero_Browser.init()` to be called before `Zotero.initialized` is true, which prevents `Zotero_Browser` from initializing. Additionally, even if `Zotero_Browser.init()` is called after Zotero is initialized in Connector mode, it is possible that `Zotero_Browser.init()` will be called _after_ the "load" event for browser.xul has already fired, so `chromeLoad` is never called. This patch ensures that both of these race conditions are taken into account.
This commit is contained in:
Aurimas Vinckevicius 2014-06-03 01:44:26 -05:00
parent 59fe54da01
commit e4dd38fc84

View file

@ -102,15 +102,45 @@ var Zotero_Browser = new function() {
* Initialize some variables and prepare event listeners for when chrome is done loading
*/
function init() {
if (!Zotero || !Zotero.initialized || !window.hasOwnProperty("gBrowser")) {
if (!window.hasOwnProperty("gBrowser")) {
return;
}
window.addEventListener("load",
function(e) { Zotero_Browser.chromeLoad(e) }, false);
var zoteroInitDone;
if (!Zotero || !Zotero.initialized) {
// Zotero either failed to load or is reloading in Connector mode
// In case of the latter, listen for the 'zotero-loaded' event (once) and retry
var zoteroInitDone_deferred = Q.defer();
var obs = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
var observer = {
"observe":function() {
obs.removeObserver(observer, 'zotero-loaded')
zoteroInitDone_deferred.resolve();
}
};
obs.addObserver(observer, 'zotero-loaded', false);
zoteroInitDone = zoteroInitDone_deferred.promise;
} else {
zoteroInitDone = Q();
}
ZoteroPane_Local.addReloadListener(reload);
reload();
var chromeLoaded = Q.defer();
window.addEventListener("load", function(e) { chromeLoaded.resolve() }, false);
// Wait for Zotero to init and chrome to load before proceeding
Q.all([
zoteroInitDone.then(function() {
ZoteroPane_Local.addReloadListener(reload);
reload();
}),
chromeLoaded.promise
])
.then(function() {
Zotero_Browser.chromeLoad()
})
.done();
}
/**