summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/dictionary.js16
-rw-r--r--ext/bg/js/translator.js38
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;
});