diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-11-12 20:20:23 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-11-12 20:20:23 -0800 |
commit | 320a82146b8673dcc1f5ba4717a8948e8f114010 (patch) | |
tree | 742fd58ddb67f2c239f8984133ec1fc6c8430921 /ext | |
parent | 371c07ab1fb6125c065589d71c93477e0456aada (diff) |
Optimization
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/database.js | 27 | ||||
-rw-r--r-- | ext/bg/js/options-form.js | 24 |
2 files changed, 35 insertions, 16 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 06786881..f3a9732c 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -21,6 +21,7 @@ class Database { constructor() { this.db = null; this.dbVer = 6; + this.entities = {}; } init() { @@ -130,13 +131,29 @@ class Database { return Promise.reject('database not initialized'); } - return this.db.entities.where('dictionary').anyOf(dictionaries).toArray(rows => { - this.entities = {}; - for (const row of rows) { - this.entities[row.name] = row.value; + const promises = []; + for (const dictionary of dictionaries) { + if (this.entities.hasOwnProperty(dictionary)) { + promises.push(Promise.resolve(this.entities[dictionary])); + } else { + const entities = this.entities[dictionary] = {}; + promises.push( + this.db.entities.where('dictionary').equals(dictionary).each(row => { + entities[row.name] = row.value; + }).then(() => entities) + ); + } + } + + return Promise.all(promises).then(results => { + const entries = {}; + for (const result of results) { + for (const name in result) { + entries[name] = result[name]; + } } - return this.entities; + return entries; }); } diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 3a201bc5..81b638b1 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -125,7 +125,7 @@ function populateDictionaries(opts) { const dictSpinner = $('#dict-spinner'); dictSpinner.show(); - database().getDictionaries().then(rows => { + return database().getDictionaries().then(rows => { rows.forEach(row => { const dictOpts = opts.dictionaries[row.title] || {enableTerms: true, enableKanji: false}; const html = Handlebars.templates['dictionary.html']({ @@ -197,16 +197,18 @@ function onDictionaryImport() { }; const dictUrl = $('#dict-url'); - database().importDictionary(dictUrl.val(), callback).then(() => { - return loadOptions().then(opts => populateDictionaries(opts)); - }).catch(error => { - dictError.show().find('span').text(error); - }).then(() => { - dictImport.prop('disabled', false); - dictUrl.val(''); - dictUrl.trigger('input'); - dictProgress.hide(); - dictSpinner.hide(); + loadOptions().then(opts => { + database().importDictionary(dictUrl.val(), callback).then(() => { + return populateDictionaries(opts); + }).catch(error => { + dictError.show().find('span').text(error); + }).then(() => { + dictImport.prop('disabled', false); + dictUrl.val(''); + dictUrl.trigger('input'); + dictProgress.hide(); + dictSpinner.hide(); + }); }); } |