Update bulkAdd implementation

This commit is contained in:
toasted-nutbread 2020-03-30 20:19:39 -04:00
parent a0c4ce779d
commit a6fedae9c7

View File

@ -322,9 +322,39 @@ class Database {
return result; 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) { async importDictionary(archiveSource, onProgress, details) {
this._validate(); this._validate();
const db = this.db;
const hasOnProgress = (typeof onProgress === 'function'); const hasOnProgress = (typeof onProgress === 'function');
// Read archive // Read archive
@ -448,11 +478,7 @@ class Database {
prefixWildcardsSupported prefixWildcardsSupported
}; };
{ await this.bulkAdd('dictionaries', [summary], 0, 1);
const transaction = db.transaction(['dictionaries'], 'readwrite');
const objectStore = transaction.objectStore('dictionaries');
await Database._bulkAdd(objectStore, [summary], 0, 1);
}
// Add data // Add data
const errors = []; const errors = [];
@ -472,9 +498,7 @@ class Database {
const count = Math.min(maxTransactionLength, ii - i); const count = Math.min(maxTransactionLength, ii - i);
try { try {
const transaction = db.transaction([objectStoreName], 'readwrite'); await this.bulkAdd(objectStoreName, entries, i, count);
const objectStore = transaction.objectStore(objectStoreName);
await Database._bulkAdd(objectStore, entries, i, count);
} catch (e) { } catch (e) {
errors.push(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) { static _open(name, version, onUpgradeNeeded) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const request = window.indexedDB.open(name, version * 10); const request = window.indexedDB.open(name, version * 10);