diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-11-03 13:24:21 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-11-10 13:01:39 -0500 |
commit | 750439ebf05768c66d8aec85993f711f97562b60 (patch) | |
tree | 4a59c57f8f768b602d3eb2ba0d170d5094ec6553 /ext/bg/js/database.js | |
parent | 6f530304693534bfac41dfbe77cbbd9c79387617 (diff) |
Use native IndexedDB for database additions
Diffstat (limited to 'ext/bg/js/database.js')
-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; + } + }); + } } |