diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-11-06 11:01:35 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-11-06 11:01:35 -0800 |
commit | 3f606a0ab00a084e7f22b232cdd5a7006ab7d636 (patch) | |
tree | 986300aaccef69f137a6395aa9dcc9b86798012d /ext/bg/js/util.js | |
parent | 84fd2d13f5d0f7321d0aa708902dd94cdbd3bac8 (diff) |
Combined import
Diffstat (limited to 'ext/bg/js/util.js')
-rw-r--r-- | ext/bg/js/util.js | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index ac365135..eaa7dd01 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -43,15 +43,6 @@ function promiseCallback(promise, callback) { }); } -function loadJson(url) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText))); - xhr.open('GET', chrome.extension.getURL(url)); - xhr.send(); - }); -} - function sortTags(tags) { return tags.sort((v1, v2) => { const order1 = v1.order; @@ -117,19 +108,48 @@ function splitField(field) { return field.length === 0 ? [] : field.split(' '); } -function importJsonDb(indexUrl, indexLoaded, entriesLoaded) { +function loadJson(url) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText))); + xhr.open('GET', chrome.extension.getURL(url)); + xhr.send(); + }); +} + +function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { if (indexLoaded !== null) { - return indexLoaded(index.title, index.version, index.entities, index.banks).then(() => index); + return indexLoaded(index.title, index.version).then(() => index); } return index; }).then(index => { const loaders = []; - for (let i = 1; i <= index.banks; ++i) { - const bankUrl = `${indexDir}/bank_${i}.json`; - loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(index.title, index.version, entries, index.banks, i))); + const banksTotal = index.termBanks + index.kanjiBanks; + let banksLoaded = 0; + + for (let i = 1; i <= index.termBanks; ++i) { + const bankUrl = `${indexDir}/term_bank_${i}.json`; + loaders.push(() => loadJson(bankUrl).then(entries => termsLoaded( + index.title, + index.version, + entries, + banksTotal, + banksLoaded++ + ))); + } + + for (let i = 1; i <= index.kanjiBanks; ++i) { + const bankUrl = `${indexDir}/kanji_bank_${i}.json`; + loaders.push(() => loadJson(bankUrl).then(entries => kanjiLoaded( + index.title, + index.version, + entries, + banksTotal, + banksLoaded++ + ))); } let chain = Promise.resolve(); |