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.version(1).stores({
terms: '++id,expression,reading',
entities: '++id,name',
kanji: '++id,character'
entities: '++,name',
kanji: '++,character'
});
return this.db;
@ -125,24 +125,24 @@ class Dictionary {
for (let i = 0; i < index.refs; ++i) {
const refUrl = `${indexDir}/ref_${i}.json`;
loaders.push(
Dictionary.loadJson(refUrl).then((refs) => {
loaders.push(() => {
return Dictionary.loadJson(refUrl).then((refs) => {
const rows = [];
for (const [e, r, t, ...g] of refs) {
rows.push({
'expression': e,
'reading': r,
'tags': t,
'glossary': g
});
for (const [expression, reading, tags, ...glossary] of refs) {
rows.push({expression, reading, tags, glossary});
}
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;
}
Translator.loadData('bg/data/rules.json').then((response) => {
this.deinflector.setRules(JSON.parse(response));
return Translator.loadData('bg/data/tags.json');
}).then((response) => {
this.tagMeta = JSON.parse(response);
Translator.loadJson('bg/data/rules.json').then((rules) => {
this.deinflector.setRules(rules);
return Translator.loadJson('bg/data/tags.json');
}).then((tagMeta) => {
this.tagMeta = tagMeta;
return this.dictionary.existsDb();
}).then((exists) => {
if (exists) {
@ -48,12 +48,12 @@ class Translator {
]);
}
}).then(() => {
this.loaded = true;
callback();
this.dictionary.findTerm('猫').then((result) => {
console.log(result);
});
this.loaded = true;
callback();
});
}
@ -246,10 +246,10 @@ class Translator {
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
}
static loadData(url) {
static loadJson(url) {
return new Promise((resolve, reject) => {
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.send();
});