diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/database.js | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 8317a91a..ab23ce2b 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -332,12 +332,19 @@ class Database { throw new Error('Unsupported dictionary version'); } - const count = await this.db.dictionaries.where('title').equals(summary.title).count(); + const db = this.db.backendDB(); + const dbCountTransaction = db.transaction(['dictionaries'], 'readonly'); + const dbIndex = dbCountTransaction.objectStore('dictionaries').index('title'); + const only = IDBKeyRange.only(summary.title); + const count = await Database.getCount(dbIndex, only); + if (count > 0) { throw new Error('Dictionary is already imported'); } - await this.db.dictionaries.add(summary); + const transaction = db.transaction(['dictionaries'], 'readwrite'); + const objectStore = transaction.objectStore('dictionaries'); + await Database.bulkAdd(objectStore, [summary], 0, 1); }; const termDataLoaded = async (summary, entries, total, current) => { @@ -712,4 +719,32 @@ class Database { request.onsuccess = () => resolve(); }); } + + static bulkAdd(objectStore, items, start, count) { + return new Promise((resolve, reject) => { + if (start + count > items.length) { + count = items.length - start; + } + + if (count <= 0) { + resolve(); + return; + } + + const end = start + count; + let completedCount = 0; + const onError = (e) => reject(e); + const onSuccess = () => { + if (++completedCount >= count) { + resolve(); + } + }; + + for (let i = start; i < end; ++i) { + const request = objectStore.add(items[i]); + request.onerror = onError; + request.onsuccess = onSuccess; + } + }); + } } |