From 2a5b026f65c93008d9ddc8681181c080aa8a3471 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 7 Jul 2015 16:36:00 -0400 Subject: [PATCH] Wait for schema update before updating save icon Technically rather than waiting for the schema update we should wait for translator initialization, which should wait for the schema update itself, but the schema update also needs to initialize the translators, so avoiding a hang is tricky, particularly with the use of Zotero.lazy() for Zotero.Translators.init(). For now, just wait for the schema update. --- chrome/content/zotero/browser.js | 26 ++++++++++++------- chrome/content/zotero/xpcom/schema.js | 4 ++- .../zotero/xpcom/translation/translators.js | 13 +++++----- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/chrome/content/zotero/browser.js b/chrome/content/zotero/browser.js index 5f11a6cd1b..a52103fdf0 100644 --- a/chrome/content/zotero/browser.js +++ b/chrome/content/zotero/browser.js @@ -44,12 +44,9 @@ var Zotero_Browser = new function() { this.toggleMode = toggleMode; this.toggleCollapsed = toggleCollapsed; this.chromeLoad = chromeLoad; - this.contentLoad = contentLoad; this.itemUpdated = itemUpdated; - this.contentHide = contentHide; this.tabClose = tabClose; this.resize = resize; - this.updateStatus = updateStatus; this.tabbrowser = null; this.appcontent = null; @@ -243,6 +240,7 @@ var Zotero_Browser = new function() { gBrowser.tabContainer.addEventListener("TabSelect", function(e) { //Zotero.debug("TabSelect"); + // Note: async Zotero_Browser.updateStatus(); }, false); // this is for pageshow, for updating the status of the book icon @@ -284,7 +282,11 @@ var Zotero_Browser = new function() { * An event handler called when a new document is loaded. Creates a new document * object, and updates the status of the capture icon */ - function contentLoad(event) { + var contentLoad = Zotero.Promise.coroutine(function* (event) { + if (Zotero.Schema.schemaUpdatePromise.isPending()) { + yield Zotero.Schema.schemaUpdatePromise; + } + var doc = event.originalTarget; var isHTML = doc instanceof HTMLDocument; var rootDoc = (doc instanceof HTMLDocument ? doc.defaultView.top.document : doc); @@ -352,12 +354,12 @@ var Zotero_Browser = new function() { contentWin.haveZoteroEventListener = true; } } - } + }); /* * called to unregister Zotero icon, etc. */ - function contentHide(event) { + this.contentHide = function (event) { var doc = event.originalTarget; if(!(doc instanceof HTMLDocument)) return; @@ -377,7 +379,8 @@ var Zotero_Browser = new function() { // update status if(Zotero_Browser.tabbrowser.selectedBrowser == browser) { - updateStatus(); + // Note: async + this.updateStatus(); } } @@ -425,7 +428,11 @@ var Zotero_Browser = new function() { * Updates the status of the capture icon to reflect the scrapability or lack * thereof of the current page */ - function updateStatus() { + this.updateStatus = Zotero.Promise.coroutine(function* () { + if (Zotero.Schema.schemaUpdatePromise.isPending()) { + yield Zotero.Schema.schemaUpdatePromise; + } + if (!Zotero_Browser.tabbrowser) return; var tab = _getTabObject(Zotero_Browser.tabbrowser.selectedBrowser); @@ -455,7 +462,7 @@ var Zotero_Browser = new function() { } else { document.getElementById('zotero-annotate-tb').hidden = true; } - } + }); function getSaveButtons() { Components.utils.import("resource:///modules/CustomizableUI.jsm"); @@ -1011,6 +1018,7 @@ Zotero_Browser.Tab.prototype._translatorsAvailable = function(translate, transla if(!translators || !translators.length) Zotero.debug("Translate: No translators found"); + // Note: async Zotero_Browser.updateStatus(); } diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 8cd8856987..c4c580f6d4 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -958,7 +958,9 @@ Zotero.Schema = new function(){ } }); - yield Zotero[Mode].reinit(cache); + yield Zotero[Mode].reinit({ + metadataCache: cache + }); return true; }); diff --git a/chrome/content/zotero/xpcom/translation/translators.js b/chrome/content/zotero/xpcom/translation/translators.js index e32ef58e3a..a3f703be36 100644 --- a/chrome/content/zotero/xpcom/translation/translators.js +++ b/chrome/content/zotero/xpcom/translation/translators.js @@ -39,13 +39,12 @@ Zotero.Translators = new function() { /** * Initializes translator cache, loading all translator metadata into memory * - * @param {Object} [memCache] - Translator metadata keyed by filename, if already available - * (e.g., in updateBundledFiles()), to avoid unnecesary file reads + * @param {Object} [options.metadataCache] - Translator metadata keyed by filename, if already + * available (e.g., in updateBundledFiles()), to avoid unnecesary file reads */ - this.reinit = Zotero.Promise.coroutine(function* (memCache) { + this.reinit = Zotero.Promise.coroutine(function* (options = {}) { Zotero.debug("Initializing translators"); var start = new Date; - _initialized = true; _cache = {"import":[], "export":[], "web":[], "search":[]}; _translators = {}; @@ -83,8 +82,8 @@ Zotero.Translators = new function() { // Check passed cache for metadata let memCacheJSON = false; - if (memCache && memCache[fileName]) { - memCacheJSON = memCache[fileName]; + if (options.metadataCache && options.metadataCache[fileName]) { + memCacheJSON = options.metadataCache[fileName]; } // Check DB cache @@ -183,6 +182,8 @@ Zotero.Translators = new function() { _cache[type].sort(cmp); } + _initialized = true; + Zotero.debug("Cached " + numCached + " translators in " + ((new Date) - start) + " ms"); }); this.init = Zotero.lazy(this.reinit);