summaryrefslogtreecommitdiff
path: root/ext/bg/js/database.js
diff options
context:
space:
mode:
authorAlex Yatskov <FooSoft@users.noreply.github.com>2019-05-05 18:26:02 -0700
committerGitHub <noreply@github.com>2019-05-05 18:26:02 -0700
commit61d1168d94a7467be6e98afc375d7583c0f23cb5 (patch)
tree383b42f3b616c033d24f62b8d883121f6687558f /ext/bg/js/database.js
parent43c5ef87c03307e8281366725f1196307effa9c7 (diff)
parentf2a5d5095931a78dc388d4cba66953560a336e7f (diff)
Merge pull request #160 from toasted-nutbread/mobile
Add support for mobile Firefox
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r--ext/bg/js/database.js68
1 files changed, 42 insertions, 26 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index 3c7f6aab..093ec102 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -228,11 +228,47 @@ class Database {
}
}
- async importDictionary(archive, callback) {
+ async importDictionary(archive, progressCallback, exceptions) {
if (!this.db) {
throw 'Database not initialized';
}
+ const maxTransactionLength = 1000;
+ const bulkAdd = async (table, items, total, current) => {
+ if (items.length < maxTransactionLength) {
+ if (progressCallback) {
+ progressCallback(total, current);
+ }
+
+ try {
+ await table.bulkAdd(items);
+ } catch (e) {
+ if (exceptions) {
+ exceptions.push(e);
+ } else {
+ throw e;
+ }
+ }
+ } else {
+ for (let i = 0; i < items.length; i += maxTransactionLength) {
+ if (progressCallback) {
+ progressCallback(total, current + i / items.length);
+ }
+
+ let count = Math.min(maxTransactionLength, items.length - i);
+ try {
+ await table.bulkAdd(items.slice(i, i + count));
+ } catch (e) {
+ if (exceptions) {
+ exceptions.push(e);
+ } else {
+ throw e;
+ }
+ }
+ }
+ }
+ };
+
const indexDataLoaded = async summary => {
if (summary.version > 3) {
throw 'Unsupported dictionary version';
@@ -247,10 +283,6 @@ class Database {
};
const termDataLoaded = async (summary, entries, total, current) => {
- if (callback) {
- callback(total, current);
- }
-
const rows = [];
if (summary.version === 1) {
for (const [expression, reading, definitionTags, rules, score, ...glossary] of entries) {
@@ -280,14 +312,10 @@ class Database {
}
}
- await this.db.terms.bulkAdd(rows);
+ await bulkAdd(this.db.terms, rows, total, current);
};
const termMetaDataLoaded = async (summary, entries, total, current) => {
- if (callback) {
- callback(total, current);
- }
-
const rows = [];
for (const [expression, mode, data] of entries) {
rows.push({
@@ -298,14 +326,10 @@ class Database {
});
}
- await this.db.termMeta.bulkAdd(rows);
+ await bulkAdd(this.db.termMeta, rows, total, current);
};
const kanjiDataLoaded = async (summary, entries, total, current) => {
- if (callback) {
- callback(total, current);
- }
-
const rows = [];
if (summary.version === 1) {
for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
@@ -332,14 +356,10 @@ class Database {
}
}
- await this.db.kanji.bulkAdd(rows);
+ await bulkAdd(this.db.kanji, rows, total, current);
};
const kanjiMetaDataLoaded = async (summary, entries, total, current) => {
- if (callback) {
- callback(total, current);
- }
-
const rows = [];
for (const [character, mode, data] of entries) {
rows.push({
@@ -350,14 +370,10 @@ class Database {
});
}
- await this.db.kanjiMeta.bulkAdd(rows);
+ await bulkAdd(this.db.kanjiMeta, rows, total, current);
};
const tagDataLoaded = async (summary, entries, total, current) => {
- if (callback) {
- callback(total, current);
- }
-
const rows = [];
for (const [name, category, order, notes, score] of entries) {
const row = dictTagSanitize({
@@ -372,7 +388,7 @@ class Database {
rows.push(row);
}
- await this.db.tagMeta.bulkAdd(rows);
+ await bulkAdd(this.db.tagMeta, rows, total, current);
};
return await Database.importDictionaryZip(