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/bg/js/translator.js | |
parent | 0e89d0e7e68d58407480e0e40e80c2f00f3c2f66 (diff) |
Progress callback for dictionary loading
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r-- | ext/bg/js/translator.js | 38 |
1 files changed, 31 insertions, 7 deletions
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; }); |