diff --git a/chrome/content/zotero/xpcom/data/feeds.js b/chrome/content/zotero/xpcom/data/feeds.js index a337f42be..d79996aa6 100644 --- a/chrome/content/zotero/xpcom/data/feeds.js +++ b/chrome/content/zotero/xpcom/data/feeds.js @@ -23,12 +23,17 @@ ***** END LICENSE BLOCK ***** */ +"use strict"; + // Mimics Zotero.Libraries Zotero.Feeds = new function() { + var _initTimeoutID; + var _initPromise; + this.init = function () { - this._timeoutID = setTimeout(() => { - this.scheduleNextFeedCheck(); - this._timeoutID = null; + _initTimeoutID = setTimeout(() => { + _initTimeoutID = null; + _initPromise = this.scheduleNextFeedCheck().then(() => _initPromise = null); }, 5000); Zotero.SyncedSettings.onSyncDownload.addListener(Zotero.Libraries.userLibraryID, 'feeds', @@ -45,9 +50,14 @@ Zotero.Feeds = new function() { }; this.uninit = function () { - if (this._timeoutID) { - clearTimeout(this._timeoutID); - this._timeoutID = null + // Cancel feed check if not yet run + if (_initTimeoutID) { + clearTimeout(_initTimeoutID); + _initTimeoutID = null + } + // Cancel feed check if in progress + if (_initPromise) { + _initPromise.cancel(); } }; diff --git a/chrome/content/zotero/xpcom/quickCopy.js b/chrome/content/zotero/xpcom/quickCopy.js index 6862c2402..934b2970c 100644 --- a/chrome/content/zotero/xpcom/quickCopy.js +++ b/chrome/content/zotero/xpcom/quickCopy.js @@ -23,18 +23,25 @@ ***** END LICENSE BLOCK ***** */ +"use strict"; Zotero.QuickCopy = new function() { + var _initTimeoutID + var _initPromise; + var _initialized = false; var _siteSettings; var _formattedNames; - var _initialized = false; this.init = Zotero.Promise.coroutine(function* () { yield this.loadSiteSettings(); // Load code for selected export translator ahead of time // (in the background, because it requires translator initialization) - setTimeout(_loadOutputFormat, 5000); + _initTimeoutID = setTimeout(() => { + _initTimeoutID = null; + _initPromise = _loadOutputFormat().then(() => _initPromise = null); + }, 5000); + if (!_initialized) { Zotero.Prefs.registerObserver("export.quickCopy.setting", () => _loadOutputFormat()); _initialized = true; @@ -42,6 +49,19 @@ Zotero.QuickCopy = new function() { }); + this.uninit = function () { + // Cancel load if not yet done + if (_initTimeoutID) { + clearTimeout(_initTimeoutID); + _initTimeoutID = null + } + // Cancel load if in progress + if (_initPromise) { + _initPromise.cancel(); + } + }; + + this.loadSiteSettings = Zotero.Promise.coroutine(function* () { var sql = "SELECT key AS domainPath, value AS format FROM settings " + "WHERE setting='quickCopySite'"; diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 48521b3e2..ba00b601d 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -633,8 +633,6 @@ Components.utils.import("resource://gre/modules/osfile.jsm"); yield Zotero.Searches.loadAll(library.libraryID); })() ); - - yield Zotero.QuickCopy.init(); } catch (e) { Zotero.logError(e); @@ -735,6 +733,10 @@ Components.utils.import("resource://gre/modules/osfile.jsm"); }; Zotero.Items.startEmptyTrashTimer(); + + yield Zotero.QuickCopy.init(); + Zotero.addShutdownListener(() => Zotero.QuickCopy.uninit()); + Zotero.Feeds.init(); Zotero.addShutdownListener(() => Zotero.Feeds.uninit()); diff --git a/test/tests/quickCopyTest.js b/test/tests/quickCopyTest.js index 62cd19e49..63365892c 100644 --- a/test/tests/quickCopyTest.js +++ b/test/tests/quickCopyTest.js @@ -40,10 +40,16 @@ describe("Zotero.QuickCopy", function() { var content = ""; var worked = false; - var format = 'export=9cb70025-a888-4a29-a210-93ec52da40d4'; // BibTeX + yield Zotero.Translators.init(); + + var translatorID = '9cb70025-a888-4a29-a210-93ec52da40d4'; // BibTeX + var format = 'export=' + translatorID; Zotero.Prefs.set('export.quickCopy.setting', format); - // The translator code is loaded automatically on pref change, but let's not wait for it - yield Zotero.QuickCopy.init(); + // Translator code for selected format is loaded automatically, so wait for it + var translator = Zotero.Translators.get(translatorID); + while (!translator.code) { + yield Zotero.Promise.delay(50); + } Zotero.QuickCopy.getContentFromItems( [item],