diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-11-05 16:44:29 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-11-05 16:44:29 -0700 |
commit | fd2820bc1af1f2a4dac477f1d2963a3b913fdb32 (patch) | |
tree | 61dafb0932bfab65682dbd28c3296a2ab5979124 /ext/bg/js/dictionary.js | |
parent | 909218c82b42812feebfac4459939176e68ba621 (diff) |
WIP
Diffstat (limited to 'ext/bg/js/dictionary.js')
-rw-r--r-- | ext/bg/js/dictionary.js | 94 |
1 files changed, 31 insertions, 63 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 1d54190e..2c9636ab 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -20,7 +20,7 @@ class Dictionary { constructor() { this.db = null; - this.dbVer = 2; + this.dbVer = 3; this.entities = null; } @@ -135,49 +135,31 @@ class Dictionary { return Promise.reject('database not initialized'); } - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return loadJson(indexUrl).then(index => { - const entities = []; - for (const [name, value] of index.ents) { - entities.push({name, value}); + const entitiesLoaded = entities => { + this.entities = entities || {}; + + const rows = []; + for (const name in entities || {}) { + rows.push({name, value: entities[name]}); } - return this.db.entities.bulkAdd(entities).then(() => { - if (this.entities === null) { - this.entities = {}; - } + return this.db.entities.bulkAdd(rows); + }; - for (const entity of entities) { - this.entities[entity.name] = entity.value; - } - }).then(() => { - const loaders = []; - for (let i = 1; i <= index.banks; ++i) { - const bankUrl = `${indexDir}/bank_${i}.json`; - loaders.push(() => { - return loadJson(bankUrl).then(definitions => { - const rows = []; - for (const [expression, reading, tags, ...glossary] of definitions) { - rows.push({expression, reading, tags, glossary}); - } - - return this.db.terms.bulkAdd(rows).then(() => { - if (callback) { - callback(i, index.banks, indexUrl); - } - }); - }); - }); - } + const entriesLoaded = (entries, total, current) => { + const rows = []; + for (const [expression, reading, tags, ...glossary] of entries) { + rows.push({expression, reading, tags, glossary}); + } - let chain = Promise.resolve(); - for (const loader of loaders) { - chain = chain.then(loader); + return this.db.terms.bulkAdd(rows).then(() => { + if (callback) { + callback(current, total, indexUrl); } - - return chain; }); - }); + }; + + return importJsonDb(indexUrl, entitiesLoaded, entriesLoaded); } importKanjiDict(indexUrl, callback) { @@ -185,33 +167,19 @@ class Dictionary { return Promise.reject('database not initialized'); } - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return loadJson(indexUrl).then(index => { - const loaders = []; - for (let i = 1; i <= index.banks; ++i) { - const bankUrl = `${indexDir}/bank_${i}.json`; - loaders.push(() => { - return loadJson(bankUrl).then(definitions => { - const rows = []; - for (const [character, onyomi, kunyomi, tags, ...meanings] of definitions) { - rows.push({character, onyomi, kunyomi, tags, meanings}); - } - - return this.db.kanji.bulkAdd(rows).then(() => { - if (callback) { - callback(i, index.banks, indexUrl); - } - }); - }); - }); + const entriesLoaded = (entries, total, current) => { + const rows = []; + for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { + rows.push({character, onyomi, kunyomi, tags, meanings}); } - let chain = Promise.resolve(); - for (const loader of loaders) { - chain = chain.then(loader); - } + return this.db.kanji.bulkAdd(rows).then(() => { + if (callback) { + callback(current, total, indexUrl); + } + }); + }; - return chain; - }); + return importJsonDb(indexUrl, null, entriesLoaded); } } |