diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-03-30 20:19:39 -0400 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-03-30 20:19:39 -0400 |
commit | a6fedae9c7fd5c3d9dfe5d20a8b10ad79af7693f (patch) | |
tree | 8b824d2ba0005efe6df433d7664eeef1639664ff | |
parent | a0c4ce779d35cab39c62ca42ad3fe58a82faa1bb (diff) |
Update bulkAdd implementation
-rw-r--r-- | ext/bg/js/database.js | 70 |
1 files changed, 33 insertions, 37 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 08a2a39f..5109e9e8 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -322,9 +322,39 @@ class Database { return result; } + bulkAdd(objectStoreName, items, start, count) { + return new Promise((resolve, reject) => { + const transaction = this.db.transaction([objectStoreName], 'readwrite'); + const objectStore = transaction.objectStore(objectStoreName); + + 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; + } + }); + } + async importDictionary(archiveSource, onProgress, details) { this._validate(); - const db = this.db; const hasOnProgress = (typeof onProgress === 'function'); // Read archive @@ -448,11 +478,7 @@ class Database { prefixWildcardsSupported }; - { - const transaction = db.transaction(['dictionaries'], 'readwrite'); - const objectStore = transaction.objectStore('dictionaries'); - await Database._bulkAdd(objectStore, [summary], 0, 1); - } + await this.bulkAdd('dictionaries', [summary], 0, 1); // Add data const errors = []; @@ -472,9 +498,7 @@ class Database { const count = Math.min(maxTransactionLength, ii - i); try { - const transaction = db.transaction([objectStoreName], 'readwrite'); - const objectStore = transaction.objectStore(objectStoreName); - await Database._bulkAdd(objectStore, entries, i, count); + await this.bulkAdd(objectStoreName, entries, i, count); } catch (e) { errors.push(e); } @@ -760,34 +784,6 @@ class Database { }); } - 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; - } - }); - } - static _open(name, version, onUpgradeNeeded) { return new Promise((resolve, reject) => { const request = window.indexedDB.open(name, version * 10); |