Stop using Zotero.lazy() for Zotero.Translators.init()

It makes things too complicated with some of the logic necessary for bundled
file updating.
This commit is contained in:
Dan Stillman 2016-12-08 19:17:49 -05:00
parent 98720462fe
commit 04516af552
2 changed files with 20 additions and 14 deletions

View file

@ -464,22 +464,22 @@ Zotero.Schema = new function(){
}
installLocation = installLocation.path;
let reinitOptions = { fromSchemaUpdate: true, noReinit: true };
let initOpts = { fromSchemaUpdate: true };
// Update files
switch (mode) {
case 'styles':
yield Zotero.Styles.reinit(reinitOptions);
yield Zotero.Styles.init(initOpts);
var updated = yield _updateBundledFilesAtLocation(installLocation, mode);
case 'translators':
yield Zotero.Translators.reinit(reinitOptions);
yield Zotero.Translators.init(initOpts);
var updated = yield _updateBundledFilesAtLocation(installLocation, mode);
default:
yield Zotero.Translators.reinit(reinitOptions);
yield Zotero.Translators.init(initOpts);
let up1 = yield _updateBundledFilesAtLocation(installLocation, 'translators', true);
yield Zotero.Styles.reinit(reinitOptions);
yield Zotero.Styles.init(initOpts);
let up2 = yield _updateBundledFilesAtLocation(installLocation, 'styles');
var updated = up1 || up2;
}

View file

@ -35,6 +35,7 @@ var TRANSLATOR_TYPES = {"import":1, "export":2, "web":4, "search":8};
Zotero.Translators = new function() {
var _cache, _translators;
var _initialized = false;
var _initializationDeferred = false;
/**
* Initializes translator cache, loading all translator metadata into memory
@ -42,18 +43,17 @@ Zotero.Translators = new function() {
* @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* (options = {}) {
this.init = Zotero.Promise.coroutine(function* (options = {}) {
if (_initializationDeferred && !options.reinit) {
return _initializationDeferred.promise;
}
_initializationDeferred = Zotero.Promise.defer();
// Wait until bundled files have been updated, except when this is called by the schema update
// code itself
if (!options.fromSchemaUpdate) {
yield Zotero.Schema.schemaUpdatePromise;
}
// Before bundled files can be updated, any existing translators need to be loaded, but other
// init() calls from elsewhere should still wait on schemaUpdatePromise, so init()/lazy()
// can't be used. Instead, the schema update code calls reinit() with noReinit.
else if (options.noReinit && _initialized) {
return;
}
Zotero.debug("Initializing translators");
var start = new Date;
@ -211,12 +211,18 @@ Zotero.Translators = new function() {
_cache[type].sort(cmp);
}
_initializationDeferred.resolve();
_initialized = true;
Zotero.debug("Cached " + numCached + " translators in " + ((new Date) - start) + " ms");
});
this.init = Zotero.lazy(this.reinit);
this.reinit = function (options = {}) {
return this.init(Object.assign({}, options, { reinit: true }));
};
/**
* Loads a translator from JSON, with optional code
*