diff --git a/content/better-bibtex.ts b/content/better-bibtex.ts index 965a782722..094b7689f9 100644 --- a/content/better-bibtex.ts +++ b/content/better-bibtex.ts @@ -357,28 +357,9 @@ Zotero.Translate.Import.prototype.Sandbox.BetterBibTeX = { $patch$(Zotero.Utilities.Internal, 'itemToExportFormat', original => function Zotero_Utilities_Internal_itemToExportFormat(zoteroItem, legacy, skipChildItems) { const start = Date.now() - try { - let serialized = Serializer.fetch(zoteroItem, !!legacy, !!skipChildItems) - if (serialized) { - log.debug('fetched serialized') - return serialized - } - - serialized = original.apply(this, arguments) - log.debug('native serialized') - - serialized = Serializer.store(zoteroItem, serialized, !!legacy, !!skipChildItems) - log.debug('stored serialized') - - return serialized - - } catch (err) { // fallback for safety for non-BBT - log.error('Zotero.Utilities.Internal.itemToExportFormat', err) - } finally { - log.debug('patched serialize took', Date.now() - start) - } - - return original.apply(this, arguments) + const serialized = original.apply(this, arguments) + log.debug('Zotero.Utilities.Internal.itemToExportFormat took', Date.now() - start) + return Serializer.enrich(serialized, zoteroItem) }) $patch$(Zotero.Translate.Export.prototype, 'translate', original => function Zotero_Translate_Export_prototype_translate() { diff --git a/content/db/cache.ts b/content/db/cache.ts index dde10e0afd..70ae5869c5 100644 --- a/content/db/cache.ts +++ b/content/db/cache.ts @@ -45,26 +45,27 @@ class Cache extends Loki { await this.loadDatabaseAsync() let coll = this.schemaCollection('itemToExportFormat', { - indices: [ 'itemID', 'legacy', 'skipChildItems' ], + indices: [ 'itemID' ], logging: false, cloneObjects: false, schema: { type: 'object', properties: { itemID: { type: 'integer' }, - legacy: { type: 'boolean' }, - skipChildItems: { type: 'boolean' }, item: { type: 'object' }, // LokiJS meta: { type: 'object' }, $loki: { type: 'integer' }, }, - required: [ 'itemID', 'legacy', 'skipChildItems', 'item' ], + required: [ 'itemID', 'item' ], additionalProperties: false, }, }) + // old cache, drop + if (coll.where(o => typeof o.legacy === 'boolean').length) coll.removeDataOnly() + clearOnUpgrade(coll, 'Zotero', Zotero.version) // this reaps unused cache entries -- make sure that cacheFetchs updates the object diff --git a/content/serializer.ts b/content/serializer.ts index 9cfd9f410e..228f67780c 100644 --- a/content/serializer.ts +++ b/content/serializer.ts @@ -16,23 +16,22 @@ export let Serializer = new class { // tslint:disable-line:variable-name this.cache = this.enabled && Cache.getCollection('itemToExportFormat') } - public fetch(item, legacy, skipChildItems) { + public fetch(item) { if (!this.cache) return null - const query = { itemID: item.id, legacy: !!legacy, skipChildItems: !!skipChildItems} - const cached = this.cache.findOne(query) + const cached = this.cache.findOne({ itemID: item.id }) if (!cached) return null return this.enrich(cached.item, item) } - public store(item, serialized, legacy, skipChildItems) { + public store(item, serialized) { // come on -- these are used in the collections export but not provided on the items?! serialized.itemID = item.id serialized.key = item.key if (this.cache) { - this.cache.insert({itemID: item.id, legacy: !!legacy, skipChildItems: !!skipChildItems, item: serialized}) + this.cache.insert({ itemID: item.id, item: serialized }) } else { if (this.enabled) Zotero.debug('Serializer.store ignored, DB not yet loaded') } @@ -43,30 +42,36 @@ export let Serializer = new class { // tslint:disable-line:variable-name public serialize(item) { return Zotero.Utilities.Internal.itemToExportFormat(item, false, true) } public fast(item) { - let serialized = item.toJSON() - serialized.uri = Zotero.URI.getItemURI(item) - serialized.itemID = item.id - - switch (serialized.itemType) { - case 'note': - break - - case 'attachment': - serialized = this.fastAttachment(serialized, item) - break - - default: - serialized.attachments = item.getAttachments().map(id => { - const att = Zotero.Items.get(id) - return this.fastAttachment({ ...att.toJSON(), uri: Zotero.URI.getItemURI(att) }, att) - }) - - serialized.notes = item.getNotes().map(id => { - const note = Zotero.Items.get(id) - return { ...note.toJSON(), uri: Zotero.URI.getItemURI(note) } - }) + let serialized = this.fetch(item) + + if (!serialized) { + serialized = item.toJSON() + serialized.uri = Zotero.URI.getItemURI(item) + serialized.itemID = item.id + + switch (serialized.itemType) { + case 'note': + break + + case 'attachment': + serialized = this.fastAttachment(serialized, item) + break + + default: + serialized.attachments = item.getAttachments().map(id => { + const att = Zotero.Items.get(id) + return this.fastAttachment({ ...att.toJSON(), uri: Zotero.URI.getItemURI(att) }, att) + }) + + serialized.notes = item.getNotes().map(id => { + const note = Zotero.Items.get(id) + return { ...note.toJSON(), uri: Zotero.URI.getItemURI(note) } + }) + } + this.store(item, serialized) } + // since the cache doesn't clone, these will be written into the cache, but since we override them always anyways, that's OK return this.enrich(serialized, item) } @@ -78,7 +83,7 @@ export let Serializer = new class { // tslint:disable-line:variable-name return serialized } - private enrich(serialized, item) { + public enrich(serialized, item) { switch (serialized.itemType) { case 'note': case 'attachment': @@ -88,9 +93,10 @@ export let Serializer = new class { // tslint:disable-line:variable-name serialized.citekey = KeyManager.get(item.id).citekey serialized.citationKey = serialized.citationKey || serialized.citekey // prepare for https://github.com/zotero/translators/pull/1810#issuecomment-456219750 serialized.journalAbbreviation = JournalAbbrev.get(serialized) - serialized.libraryID = item.libraryID break } + + serialized.libraryID = item.libraryID return serialized } } diff --git a/content/translators.ts b/content/translators.ts index dc1805c066..5944f918f7 100644 --- a/content/translators.ts +++ b/content/translators.ts @@ -356,7 +356,7 @@ export let Translators = new class { // tslint:disable-line:variable-name prep.exported = (Object.keys(config.cache).length * 100) / config.items.length // tslint:disable-line:no-magic-numbers try { - worker.postMessage(JSON.parse(JSON.stringify(config, (name, val) => typeof val === 'function' ? undefined : val))) + worker.postMessage(JSON.parse(JSON.stringify(config))) } catch (err) { worker.terminate() this.workers.running.delete(id)