From a901b47d878407172247cd60768087ef4f474bd7 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 15 Aug 2013 22:30:33 -0400 Subject: [PATCH] Use async file access for saving translators and styles --- chrome/content/zotero/xpcom/schema.js | 102 ++++++++++-------- .../zotero/xpcom/translation/translator.js | 49 +++++---- 2 files changed, 87 insertions(+), 64 deletions(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index b365ebf21..316324ff0 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -992,20 +992,28 @@ Zotero.Schema = new function(){ } var body = 'styles=' + encodeURIComponent(JSON.stringify(styleTimestamps)); - Zotero.HTTP.promise("POST", url, { body: body }) + return Zotero.HTTP.promise("POST", url, { body: body }) .then(function (xmlhttp) { return _updateFromRepositoryCallback(xmlhttp, !!force); }) .catch(function (e) { - if (e instanceof Zotero.HTTP.BrowserOfflineException) { - Zotero.debug('Browser is offline -- skipping check'); + if (e instanceof Zotero.HTTP.BrowserOfflineException || e.xmlhttp) { + var msg = " -- retrying in " + ZOTERO_CONFIG.REPOSITORY_RETRY_INTERVAL + if (e instanceof Zotero.HTTP.BrowserOfflineException) { + Zotero.debug("Browser is offline" + msg); + } + else { + Components.utils.reportError(e); + Zotero.debug("Error updating from repository " + msg); + } // TODO: instead, add an observer to start and stop timer on online state change _setRepositoryTimer(ZOTERO_CONFIG.REPOSITORY_RETRY_INTERVAL); return; } throw e; }); - }); + }) + .finally(function () _remoteUpdateInProgress = false); }); } @@ -1509,7 +1517,6 @@ Zotero.Schema = new function(){ _setRepositoryTimer(ZOTERO_CONFIG['REPOSITORY_RETRY_INTERVAL']); } - _remoteUpdateInProgress = false; return Q(false); } @@ -1532,47 +1539,51 @@ Zotero.Schema = new function(){ if (!manual) { _setRepositoryTimer(ZOTERO_CONFIG['REPOSITORY_CHECK_INTERVAL']); } - _remoteUpdateInProgress = false; - return -1; + return Q(true); }); } - try { - for (var i=0, len=translatorUpdates.length; i4K chunks into multiple nodes // https://bugzilla.mozilla.org/show_bug.cgi?id=194231 @@ -1727,8 +1740,7 @@ Zotero.Schema = new function(){ } Zotero.debug("Saving style '" + uri + "'"); - Zotero.File.putContents(destFile, str); - return; + return Zotero.File.putContentsAsync(destFile, str); } diff --git a/chrome/content/zotero/xpcom/translation/translator.js b/chrome/content/zotero/xpcom/translation/translator.js index d933b821d..9c5137202 100644 --- a/chrome/content/zotero/xpcom/translation/translator.js +++ b/chrome/content/zotero/xpcom/translation/translator.js @@ -331,7 +331,7 @@ Zotero.Translators = new function() { * @param {Boolean} metadata.inRepository * @param {String} metadata.lastUpdated SQL date * @param {String} code - * @return {nsIFile} + * @return {Promise} */ this.save = function(metadata, code) { if (!metadata.translatorID) { @@ -373,7 +373,7 @@ Zotero.Translators = new function() { var destFile = Zotero.getTranslatorsDirectory(); destFile.append(fileName); - // JSON.stringify (FF 3.5.4 and up) has the benefit of indenting JSON + // JSON.stringify has the benefit of indenting JSON var metadataJSON = JSON.stringify(metadata, null, "\t"); var str = metadataJSON + "\n\n" + code; @@ -383,23 +383,34 @@ Zotero.Translators = new function() { var sameFile = true; } - if (!sameFile && destFile.exists()) { - var msg = "Overwriting translator with same filename '" - + fileName + "'"; - Zotero.debug(msg, 1); - Zotero.debug(metadata, 1); - Components.utils.reportError(msg + " in Zotero.Translators.save()"); - } - - if (translator && translator.file.exists()) { - translator.file.remove(false); - } - - Zotero.debug("Saving translator '" + metadata.label + "'"); - Zotero.debug(str); - Zotero.File.putContents(destFile, str); - - return destFile; + return Q.fcall(function () { + if (sameFile) return; + + return Q(OS.File.exists(destFile.path)) + .then(function (exists) { + if (exists) { + var msg = "Overwriting translator with same filename '" + + fileName + "'"; + Zotero.debug(msg, 1); + Zotero.debug(metadata, 1); + Components.utils.reportError(msg); + } + }); + }) + .then(function () { + if (!translator) return; + + return Q(OS.File.exists(translator.file.path)) + .then(function (exists) { + translator.file.remove(false); + }); + }) + .then(function () { + Zotero.debug("Saving translator '" + metadata.label + "'"); + Zotero.debug(str); + return Zotero.File.putContentsAsync(destFile, str) + .thenResolve(destFile); + }); } this.cacheInDB = function(fileName, metadataJSON, code, lastModifiedTime) {