Optimization

This commit is contained in:
Alex Yatskov 2016-11-12 20:20:23 -08:00
parent 371c07ab1f
commit 320a82146b
2 changed files with 35 additions and 16 deletions

View File

@ -21,6 +21,7 @@ class Database {
constructor() { constructor() {
this.db = null; this.db = null;
this.dbVer = 6; this.dbVer = 6;
this.entities = {};
} }
init() { init() {
@ -130,13 +131,29 @@ class Database {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
return this.db.entities.where('dictionary').anyOf(dictionaries).toArray(rows => { const promises = [];
this.entities = {}; for (const dictionary of dictionaries) {
for (const row of rows) { if (this.entities.hasOwnProperty(dictionary)) {
this.entities[row.name] = row.value; 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;
}); });
} }

View File

@ -125,7 +125,7 @@ function populateDictionaries(opts) {
const dictSpinner = $('#dict-spinner'); const dictSpinner = $('#dict-spinner');
dictSpinner.show(); dictSpinner.show();
database().getDictionaries().then(rows => { return database().getDictionaries().then(rows => {
rows.forEach(row => { rows.forEach(row => {
const dictOpts = opts.dictionaries[row.title] || {enableTerms: true, enableKanji: false}; const dictOpts = opts.dictionaries[row.title] || {enableTerms: true, enableKanji: false};
const html = Handlebars.templates['dictionary.html']({ const html = Handlebars.templates['dictionary.html']({
@ -197,16 +197,18 @@ function onDictionaryImport() {
}; };
const dictUrl = $('#dict-url'); const dictUrl = $('#dict-url');
database().importDictionary(dictUrl.val(), callback).then(() => { loadOptions().then(opts => {
return loadOptions().then(opts => populateDictionaries(opts)); database().importDictionary(dictUrl.val(), callback).then(() => {
}).catch(error => { return populateDictionaries(opts);
dictError.show().find('span').text(error); }).catch(error => {
}).then(() => { dictError.show().find('span').text(error);
dictImport.prop('disabled', false); }).then(() => {
dictUrl.val(''); dictImport.prop('disabled', false);
dictUrl.trigger('input'); dictUrl.val('');
dictProgress.hide(); dictUrl.trigger('input');
dictSpinner.hide(); dictProgress.hide();
dictSpinner.hide();
});
}); });
} }