This commit is contained in:
Alex Yatskov 2016-08-23 20:53:11 -07:00
parent f106b64876
commit 6366d9bd8e
2 changed files with 24 additions and 24 deletions

View File

@ -38,8 +38,8 @@ class Dictionary {
this.db = new Dexie('dict'); this.db = new Dexie('dict');
this.db.version(1).stores({ this.db.version(1).stores({
terms: '++id,expression,reading', terms: '++id,expression,reading',
entities: '++id,name', entities: '++,name',
kanji: '++id,character' kanji: '++,character'
}); });
return this.db; return this.db;
@ -125,24 +125,24 @@ class Dictionary {
for (let i = 0; i < index.refs; ++i) { for (let i = 0; i < index.refs; ++i) {
const refUrl = `${indexDir}/ref_${i}.json`; const refUrl = `${indexDir}/ref_${i}.json`;
loaders.push( loaders.push(() => {
Dictionary.loadJson(refUrl).then((refs) => { return Dictionary.loadJson(refUrl).then((refs) => {
const rows = []; const rows = [];
for (const [e, r, t, ...g] of refs) { for (const [expression, reading, tags, ...glossary] of refs) {
rows.push({ rows.push({expression, reading, tags, glossary});
'expression': e,
'reading': r,
'tags': t,
'glossary': g
});
} }
return this.db.terms.bulkAdd(rows); return this.db.terms.bulkAdd(rows);
}) });
); });
} }
return Promise.all(loaders); let chain = Promise.resolve();
for (const loader of loaders) {
chain = chain.then(loader);
}
return chain;
}); });
}); });
} }

View File

@ -31,11 +31,11 @@ class Translator {
return; return;
} }
Translator.loadData('bg/data/rules.json').then((response) => { Translator.loadJson('bg/data/rules.json').then((rules) => {
this.deinflector.setRules(JSON.parse(response)); this.deinflector.setRules(rules);
return Translator.loadData('bg/data/tags.json'); return Translator.loadJson('bg/data/tags.json');
}).then((response) => { }).then((tagMeta) => {
this.tagMeta = JSON.parse(response); this.tagMeta = tagMeta;
return this.dictionary.existsDb(); return this.dictionary.existsDb();
}).then((exists) => { }).then((exists) => {
if (exists) { if (exists) {
@ -48,12 +48,12 @@ class Translator {
]); ]);
} }
}).then(() => { }).then(() => {
this.loaded = true;
callback();
this.dictionary.findTerm('猫').then((result) => { this.dictionary.findTerm('猫').then((result) => {
console.log(result); console.log(result);
}); });
this.loaded = true;
callback();
}); });
} }
@ -246,10 +246,10 @@ class Translator {
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
} }
static loadData(url) { static loadJson(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => resolve(xhr.responseText)); xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
xhr.open('GET', chrome.extension.getURL(url), true); xhr.open('GET', chrome.extension.getURL(url), true);
xhr.send(); xhr.send();
}); });