commit a901b47d878407172247cd60768087ef4f474bd7
parent 28ca0a3599308c513feb55f12f1904a790e1dcca
Author: Dan Stillman <dstillman@zotero.org>
Date: Thu, 15 Aug 2013 22:30:33 -0400
Use async file access for saving translators and styles
Diffstat:
2 files changed, 86 insertions(+), 63 deletions(-)
diff --git 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; i<len; i++){
- _translatorXMLToFile(translatorUpdates[i]);
- }
-
- for (var i=0, len=styleUpdates.length; i<len; i++){
- _styleXMLToFile(styleUpdates[i]);
+ return Q.async(function () {
+ try {
+ for (var i=0, len=translatorUpdates.length; i<len; i++){
+ yield _translatorXMLToFile(translatorUpdates[i]);
+ }
+
+ for (var i=0, len=styleUpdates.length; i<len; i++){
+ yield _styleXMLToFile(styleUpdates[i]);
+ }
+
+ // Rebuild caches
+ Zotero.Translators.init();
+ Zotero.Styles.init();
}
-
- // Rebuild caches
- Zotero.Translators.init();
- Zotero.Styles.init();
- }
- catch (e) {
- Zotero.debug(e, 1);
- if (!manual){
- _setRepositoryTimer(ZOTERO_CONFIG['REPOSITORY_RETRY_INTERVAL']);
+ catch (e) {
+ Zotero.debug(e, 1);
+ if (!manual){
+ _setRepositoryTimer(ZOTERO_CONFIG['REPOSITORY_RETRY_INTERVAL']);
+ }
+ Q.return(false);
}
- _remoteUpdateInProgress = false;
- return Q(false);
- }
-
- return Zotero.DB.executeTransaction(function (conn) {
- // Store the timestamp provided by the server
- yield _updateDBVersion('repository', currentTime);
- // And the local timestamp of the update time
- yield _updateDBVersion('lastcheck', lastCheckTime);
- })
- .then(function () {
- if (!manual) {
- _setRepositoryTimer(ZOTERO_CONFIG['REPOSITORY_CHECK_INTERVAL']);
- }
- _remoteUpdateInProgress = false;
+ Q.return(true);
+ })()
+ .then(function (update) {
+ if (!update) return false;
- return true;
+ return Zotero.DB.executeTransaction(function (conn) {
+ // Store the timestamp provided by the server
+ yield _updateDBVersion('repository', currentTime);
+
+ // And the local timestamp of the update time
+ yield _updateDBVersion('lastcheck', lastCheckTime);
+ })
+ .then(function () {
+ if (!manual) {
+ _setRepositoryTimer(ZOTERO_CONFIG['REPOSITORY_CHECK_INTERVAL']);
+ }
+
+ return true;
+ });
});
}
@@ -1606,9 +1617,11 @@ Zotero.Schema = new function(){
/**
- * Traverse an XML translator node from the repository and
- * update the local translators folder with the translator data
- **/
+ * Traverse an XML translator node from the repository and
+ * update the local translators folder with the translator data
+ *
+ * @return {Promise}
+ */
function _translatorXMLToFile(xmlnode) {
// Don't split >4K 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
@@ -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<nsIFile>}
*/
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) {