From a6fedae9c7fd5c3d9dfe5d20a8b10ad79af7693f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 30 Mar 2020 20:19:39 -0400 Subject: Update bulkAdd implementation --- ext/bg/js/database.js | 70 ++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 37 deletions(-) (limited to 'ext/bg') 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); -- cgit v1.2.3