summaryrefslogtreecommitdiff
path: root/ext/bg/js/database.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-09-12 13:29:13 -0700
committerAlex Yatskov <alex@foosoft.net>2017-09-12 13:29:13 -0700
commitaf93d446de6548eb3753a5ca36c42d9c8c8a3730 (patch)
treec83adae9c0d479e9f91e7795e70070cff79ff2be /ext/bg/js/database.js
parentbe87e916df3a2f26e32e3861ce3430aaaa2ea67e (diff)
fix dictionary importing
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r--ext/bg/js/database.js73
1 files changed, 39 insertions, 34 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index c346da4e..71179ed6 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -144,22 +144,6 @@ class Database {
return Promise.reject('database not initialized');
}
- // const rows = [];
- // for (const tag in tagMeta || {}) {
- // const meta = tagMeta[tag];
- // const row = dictTagSanitize({
- // name: tag,
- // category: meta.category,
- // notes: meta.notes,
- // order: meta.order,
- // dictionary: title
- // });
-
- // rows.push(row);
- // }
-
- // await this.db.tagMeta.bulkAdd(rows);
-
const indexDataLoaded = async summary => {
const count = await this.db.dictionaries.where('title').equals(summary.title).count();
if (count > 0) {
@@ -210,6 +194,27 @@ class Database {
await this.db.kanji.bulkAdd(rows);
};
+ const tagDataLoaded = async (title, entries, total, current) => {
+ if (callback) {
+ callback(total, current);
+ }
+
+ const rows = [];
+ for (const [name, category, order, notes] of entries) {
+ const row = dictTagSanitize({
+ name,
+ category,
+ order,
+ notes,
+ dictionary: title
+ });
+
+ rows.push(row);
+ }
+
+ await this.db.tagMeta.bulkAdd(rows);
+ };
+
return await Database.importDictionaryZip(
archive,
indexDataLoaded,
@@ -217,7 +222,7 @@ class Database {
null,
kanjiDataLoaded,
null,
- null
+ tagDataLoaded
);
}
@@ -247,16 +252,6 @@ class Database {
await indexDataLoaded(summary);
}
- if (tagDataLoaded && index.tagMeta) {
- const tags = [];
- for (const name of index.tagMeta) {
- const tag = index.tagMeta;
- tags.push([name, tag.category, tag.order, tag.notes]);
- }
-
- tagDataLoaded(tags);
- }
-
const buildTermBankName = index => `term_bank_${index + 1}.json`;
const buildTermFreqBankName = index => `termfreq_bank_${index + 1}.json`;
const buildKanjiBankName = index => `kanji_bank_${index + 1}.json`;
@@ -273,23 +268,33 @@ class Database {
};
const termBankCount = countBanks(buildTermBankName);
- const kanjiBankCount = countBanks(buildTermBankName);
- const termFreqBankCount = countBanks(buildTermBankName);
- const kanjiFreqBankCount = countBanks(buildTermBankName);
- const tagBankCount = countBanks(buildTermBankName);
+ const termFreqBankCount = countBanks(buildTermFreqBankName);
+ const kanjiBankCount = countBanks(buildKanjiBankName);
+ const kanjiFreqBankCount = countBanks(buildKanjiFreqBankName);
+ const tagBankCount = countBanks(buildTagBankName);
let bankLoadedCount = 0;
- const bankTotalCount =
+ let bankTotalCount =
termBankCount +
- hanjiBankCount +
termFreqBankCount +
+ kanjiBankCount +
kanjiFreqBankCount +
tagBankCount;
+ if (tagDataLoaded && index.tagMeta) {
+ const bank = [];
+ for (const name in index.tagMeta) {
+ const tag = index.tagMeta[name];
+ bank.push([name, tag.category, tag.order, tag.notes]);
+ }
+
+ tagDataLoaded(index.title, bank, ++bankTotalCount, bankLoadedCount++);
+ }
+
const loadBank = async (namer, count, callback) => {
if (callback) {
for (let i = 0; i < count; ++i) {
- const bankFile = namer(i);
+ const bankFile = files[namer(i)];
const bank = JSON.parse(await bankFile.async('string'));
await callback(index.title, bank, bankTotalCount, bankLoadedCount++);
}