diff options
-rw-r--r-- | ext/bg/js/database.js | 32 | ||||
-rw-r--r-- | ext/bg/js/translator.js | 21 |
2 files changed, 28 insertions, 25 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index cd53f681..6ae36db2 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -20,7 +20,6 @@ class Database { constructor() { this.db = null; - this.tagCache = {}; } async prepare() { @@ -38,7 +37,7 @@ class Database { this.db.version(3).stores({ termFreq: '++,dictionary,expression', kanjiFreq: '++,dictionary,character', - tagMeta: '++,dictionary' + tagMeta: '++,dictionary,name' }); await this.db.open(); @@ -52,7 +51,6 @@ class Database { this.db.close(); await this.db.delete(); this.db = null; - this.tagCache = {}; await this.prepare(); } @@ -78,11 +76,6 @@ class Database { } }); - await this.cacheTagMeta(titles); - for (const result of results) { - result.tagMeta = this.tagCache[result.dictionary] || {}; - } - return results; } @@ -120,11 +113,6 @@ class Database { } }); - await this.cacheTagMeta(titles); - for (const result of results) { - result.tagMeta = this.tagCache[result.dictionary] || {}; - } - return results; } @@ -143,21 +131,19 @@ class Database { return results; } - async cacheTagMeta(titles) { + async findTag(name, titles) { if (!this.db) { throw 'database not initialized'; } - for (const title of titles) { - if (!this.tagCache[title]) { - const tagMeta = {}; - await this.db.tagMeta.where('dictionary').equals(title).each(row => { - tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order}; - }); - - this.tagCache[title] = tagMeta; + let result = null; + await this.db.tagMeta.where('name').equals(name).each(row => { + if (titles.includes(row.dictionary)) { + result = row; } - } + }); + + return result; } async getDictionaries() { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index a2322e36..99147618 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -60,7 +60,7 @@ class Translator { let definitions = []; for (const deinflection of deinflections) { for (const definition of deinflection.definitions) { - const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); + const tags = await this.buildTags(definition.tags, titles); tags.push(dictTagBuildSource(definition.dictionary)); let frequencies = await this.database.findTermFreq(definition.expression, titles); @@ -124,12 +124,29 @@ class Translator { } for (const definition of definitions) { - const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta)); + const tags = await this.buildTags(definition.tags, titles); tags.push(dictTagBuildSource(definition.dictionary)); + definition.tags = dictTagsSort(tags); definition.frequencies = await this.database.findKanjiFreq(definition.character, titles); } return definitions; } + + async buildTags(names, titles) { + const results = []; + for (const name of names) { + const meta = await this.database.findTag(name.split(':')[0], titles); + + const result = {name}; + for (const prop in meta || {}) { + result[prop] = meta[prop]; + } + + results.push(dictTagSanitize(result)); + } + + return results; + } } |