diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-09-13 20:36:13 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-09-13 20:36:13 -0700 |
commit | 17366e521afe2dd3b32f2068db43f972ce89b36f (patch) | |
tree | 94a1a2c37e8187ecc2484be79b6d5d032344b648 /ext | |
parent | 0e89d0e7e68d58407480e0e40e80c2f00f3c2f66 (diff) |
Progress callback for dictionary loading
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/dictionary.js | 16 | ||||
-rw-r--r-- | ext/bg/js/translator.js | 38 |
2 files changed, 43 insertions, 11 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 936dc3c1..2bd39344 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -130,7 +130,7 @@ class Dictionary { }); } - importTermDict(indexUrl) { + importTermDict(indexUrl, callback) { if (this.db === null) { return Promise.reject('database not initialized'); } @@ -161,7 +161,11 @@ class Dictionary { rows.push({expression, reading, tags, glossary}); } - return this.db.terms.bulkAdd(rows); + return this.db.terms.bulkAdd(rows).then(() => { + if (callback) { + callback(i, index.banks); + } + }); }); }); } @@ -176,7 +180,7 @@ class Dictionary { }); } - importKanjiDict(indexUrl) { + importKanjiDict(indexUrl, callback) { if (this.db === null) { return Promise.reject('database not initialized'); } @@ -193,7 +197,11 @@ class Dictionary { rows.push({character, onyomi, kunyomi, tags, meanings}); } - return this.db.kanji.bulkAdd(rows); + return this.db.kanji.bulkAdd(rows).then(() => { + if (callback) { + callback(i, index.banks); + } + }); }); }); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 2cc97e1c..1dc0dca1 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -25,7 +25,7 @@ class Translator { this.deinflector = new Deinflector(); } - loadData() { + loadData(callback) { if (this.loaded) { return Promise.resolve(); } @@ -37,13 +37,37 @@ class Translator { this.tagMeta = tagMeta; return this.dictionary.prepareDb(); }).then(exists => { - if (!exists) { - return Promise.all([ - this.dictionary.importKanjiDict('bg/data/kanjidic/index.json'), - this.dictionary.importTermDict('bg/data/edict/index.json'), - this.dictionary.importTermDict('bg/data/enamdict/index.json') - ]).then(() => this.dictionary.sealDb()); + if (exists) { + return; } + + if (callback) { + callback({state: 'begin', progress: 0}); + } + + let banksLoaded = 0; + let banksTotal = 0; + + const bankCallback = (loaded, total) => { + banksLoaded += loaded; + banksTotal += total; + + if (callback) { + callback({state: 'update', progress: Math.ceil(100 * banksLoaded / banksTotal)}); + } + }; + + return Promise.all([ + this.dictionary.importTermDict('bg/data/edict/index.json', bankCallback), + this.dictionary.importTermDict('bg/data/enamdict/index.json', bankCallback), + this.dictionary.importKanjiDict('bg/data/kanjidic/index.json', bankCallback), + ]).then(() => { + return this.dictionary.sealDb(); + }).then(() => { + if (callback) { + callback({state: 'end', progress: 100}); + } + }); }).then(() => { this.loaded = true; }); |