Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Large Dictionaries Failing to Delete #375

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions ext/js/data/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ export class Database {
if (typeof filterKeys === 'function') {
keys = filterKeys(keys);
}
this._bulkDeleteInternal(objectStore, keys, onProgress);
transaction.commit();
this._bulkDeleteInternal(objectStore, keys, onProgress, transaction);
} catch (e) {
reject(e);
}
Expand Down Expand Up @@ -448,28 +447,43 @@ export class Database {
* @param {IDBObjectStore} objectStore
* @param {IDBValidKey[]} keys
* @param {?(completedCount: number, totalCount: number) => void} onProgress
* @param {() => void} onCompletion
* @param {number} index
*/
_bulkDeleteInternal(objectStore, keys, onProgress) {
const count = keys.length;
if (count === 0) { return; }
_deleteKeySequentially(objectStore, keys, onProgress, onCompletion, index) {
if (index >= keys.length) {
onCompletion();
return;
}

let completedCount = 0;
const onSuccess = () => {
++completedCount;
try {
/** @type {(completedCount: number, totalCount: number) => void}} */ (onProgress)(completedCount, count);
} catch (e) {
// NOP
const key = keys[index];
const request = objectStore.delete(key);

request.onsuccess = () => {
if (typeof onProgress === 'function') {
onProgress(index + 1, keys.length);
}
this._deleteKeySequentially(objectStore, keys, onProgress, onCompletion, index + 1);
};
}

const hasProgress = (typeof onProgress === 'function');
for (const key of keys) {
const request = objectStore.delete(key);
if (hasProgress) {
request.onsuccess = onSuccess;
/**
* @param {IDBObjectStore} objectStore
* @param {IDBValidKey[]} keys
* @param {?(completedCount: number, totalCount: number) => void} onProgress
* @param {IDBTransaction} transaction
*/
_bulkDeleteInternal(objectStore, keys, onProgress, transaction) {
const count = keys.length;
if (count === 0) { return; }
const onCompletion = () => {
if (typeof onProgress === 'function') {
onProgress(count, count);
}
}
transaction.commit();
};

this._deleteKeySequentially(objectStore, keys, onProgress, onCompletion, 0);
}

/**
Expand Down