From 6366d9bd8e5758d631e5bba14b2c892e0b27c474 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 23 Aug 2016 20:53:11 -0700 Subject: [PATCH] WIP --- ext/bg/js/dictionary.js | 28 ++++++++++++++-------------- ext/bg/js/translator.js | 20 ++++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index dd46064a..44e97752 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -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; }); }); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f9ac1d56..8af0e31b 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -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(); });