aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/translator.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-11-13 11:58:23 -0800
committerAlex Yatskov <alex@foosoft.net>2016-11-13 11:58:23 -0800
commitdab46eda3e42e5f0a624bb285a555b078b999a99 (patch)
treede2729f5370cdc5ed0af544fe743fb9ea6b2e3c4 /ext/bg/js/translator.js
parent7598de2234393433e98c9af3c312a1bcfd82c32f (diff)
WIP
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r--ext/bg/js/translator.js52
1 files changed, 9 insertions, 43 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index ed8c720b..01213bed 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -25,54 +25,20 @@ class Translator {
this.deinflector = new Deinflector();
}
- loadData(callback) {
+ prepare() {
if (this.loaded) {
return Promise.resolve();
}
- return loadJsonInt('bg/data/rules.json').then(rules => {
- this.deinflector.setRules(rules);
- return loadJsonInt('bg/data/tags.json');
- }).then(tagMeta => {
- this.tagMeta = tagMeta;
- return this.database.prepare();
- }).then(exists => {
- if (exists) {
- return;
- }
-
- if (callback) {
- callback({state: 'begin', progress: 0});
- }
-
- const banks = {};
- const bankCallback = (total, loaded, indexUrl) => {
- banks[indexUrl] = {loaded, total};
-
- let percent = 0.0;
- for (const url in banks) {
- percent += banks[url].loaded / banks[url].total;
- }
+ const promises = [
+ loadJsonInt('bg/data/rules.json'),
+ loadJsonInt('bg/data/tags.json'),
+ this.database.prepare()
+ ];
- percent /= 3.0;
-
- if (callback) {
- callback({state: 'update', progress: Math.ceil(100.0 * percent)});
- }
- };
-
- return Promise.all([
- this.database.importDictionary(chrome.extension.getURL('bg/data/edict/index.json'), bankCallback),
- this.database.importDictionary(chrome.extension.getURL('bg/data/enamdict/index.json'), bankCallback),
- this.database.importDictionary(chrome.extension.getURL('bg/data/kanjidic/index.json'), bankCallback),
- ]).then(() => {
- return this.database.seal();
- }).then(() => {
- if (callback) {
- callback({state: 'end', progress: 100.0});
- }
- });
- }).then(() => {
+ return Promise.all(promises).then(([rules, tags]) => {
+ this.deinflector.setRules(rules);
+ this.tagMeta = tags;
this.loaded = true;
});
}