aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-08-13 18:35:31 -0400
committerGitHub <noreply@github.com>2021-08-13 18:35:31 -0400
commitcd78d56feedef1475ce8ddb98e208c7a004819df (patch)
tree0584c50b423fb366074bc53919da8f96258066f6
parent9facacfb23a03c63da4c1cd8c7514b5fd3d3b0f9 (diff)
Database improvements to data persistency (#1893)
* Fix function check * Rename arguments of getAll, getAllKeys, and their private functions * Simplify bulkAdd * Simplify delete * Simplify bulkDelete * Remove persistData
-rw-r--r--ext/js/data/database.js108
-rw-r--r--ext/js/language/dictionary-database.js4
-rw-r--r--ext/js/language/dictionary-importer.js4
3 files changed, 43 insertions, 73 deletions
diff --git a/ext/js/data/database.js b/ext/js/data/database.js
index 222e376c..224233a8 100644
--- a/ext/js/data/database.js
+++ b/ext/js/data/database.js
@@ -76,38 +76,30 @@ class Database {
return;
}
- const end = start + count;
- let completedCount = 0;
- const onError = (e) => reject(e.target.error);
- const onSuccess = () => {
- if (++completedCount >= count) {
- resolve();
- }
- };
-
const transaction = this.transaction([objectStoreName], 'readwrite');
+ transaction.onerror = (e) => reject(e.target.error);
+ transaction.oncomplete = () => resolve();
const objectStore = transaction.objectStore(objectStoreName);
- for (let i = start; i < end; ++i) {
- const request = objectStore.add(items[i]);
- request.onerror = onError;
- request.onsuccess = onSuccess;
+ for (let i = start, ii = start + count; i < ii; ++i) {
+ objectStore.add(items[i]);
}
+ transaction.commit();
});
}
- getAll(objectStoreOrIndex, query, resolve, reject, data) {
+ getAll(objectStoreOrIndex, query, onSuccess, onError, data) {
if (typeof objectStoreOrIndex.getAll === 'function') {
- this._getAllFast(objectStoreOrIndex, query, resolve, reject, data);
+ this._getAllFast(objectStoreOrIndex, query, onSuccess, onError, data);
} else {
- this._getAllUsingCursor(objectStoreOrIndex, query, resolve, reject, data);
+ this._getAllUsingCursor(objectStoreOrIndex, query, onSuccess, onError, data);
}
}
- getAllKeys(objectStoreOrIndex, query, resolve, reject) {
- if (typeof objectStoreOrIndex.getAll === 'function') {
- this._getAllKeysFast(objectStoreOrIndex, query, resolve, reject);
+ getAllKeys(objectStoreOrIndex, query, onSuccess, onError) {
+ if (typeof objectStoreOrIndex.getAllKeys === 'function') {
+ this._getAllKeysFast(objectStoreOrIndex, query, onSuccess, onError);
} else {
- this._getAllKeysUsingCursor(objectStoreOrIndex, query, resolve, reject);
+ this._getAllKeysUsingCursor(objectStoreOrIndex, query, onSuccess, onError);
}
}
@@ -170,16 +162,20 @@ class Database {
delete(objectStoreName, key) {
return new Promise((resolve, reject) => {
const transaction = this.transaction([objectStoreName], 'readwrite');
+ transaction.onerror = (e) => reject(e.target.error);
+ transaction.oncomplete = () => resolve();
const objectStore = transaction.objectStore(objectStoreName);
- const request = objectStore.delete(key);
- request.onerror = (e) => reject(e.target.error);
- request.onsuccess = () => resolve();
+ objectStore.delete(key);
+ transaction.commit();
});
}
bulkDelete(objectStoreName, indexName, query, filterKeys=null, onProgress=null) {
return new Promise((resolve, reject) => {
const transaction = this.transaction([objectStoreName], 'readwrite');
+ transaction.onerror = (e) => reject(e.target.error);
+ transaction.oncomplete = () => resolve();
+
const objectStore = transaction.objectStore(objectStoreName);
const objectStoreOrIndex = indexName !== null ? objectStore.index(indexName) : objectStore;
@@ -188,7 +184,8 @@ class Database {
if (typeof filterKeys === 'function') {
keys = filterKeys(keys);
}
- this._bulkDeleteInternal(objectStore, keys, onProgress, resolve, reject);
+ this._bulkDeleteInternal(objectStore, keys, onProgress);
+ transaction.commit();
} catch (e) {
reject(e);
}
@@ -198,16 +195,6 @@ class Database {
});
}
- persistData(objectStoreName) {
- return new Promise((resolve, reject) => {
- const transaction = this.transaction([objectStoreName], 'readonly');
- const objectStore = transaction.objectStore(objectStoreName);
- const cursor = objectStore.openCursor();
- cursor.onerror = (e) => reject(e.target.error);
- cursor.onsuccess = () => resolve();
- });
- }
-
static deleteDatabase(databaseName) {
return new Promise((resolve, reject) => {
const request = indexedDB.deleteDatabase(databaseName);
@@ -266,77 +253,68 @@ class Database {
return false;
}
- _getAllFast(objectStoreOrIndex, query, resolve, reject, data) {
+ _getAllFast(objectStoreOrIndex, query, onSuccess, onReject, data) {
const request = objectStoreOrIndex.getAll(query);
- request.onerror = (e) => reject(e.target.error, data);
- request.onsuccess = (e) => resolve(e.target.result, data);
+ request.onerror = (e) => onReject(e.target.error, data);
+ request.onsuccess = (e) => onSuccess(e.target.result, data);
}
- _getAllUsingCursor(objectStoreOrIndex, query, resolve, reject, data) {
+ _getAllUsingCursor(objectStoreOrIndex, query, onSuccess, onReject, data) {
const results = [];
const request = objectStoreOrIndex.openCursor(query, 'next');
- request.onerror = (e) => reject(e.target.error, data);
+ request.onerror = (e) => onReject(e.target.error, data);
request.onsuccess = (e) => {
const cursor = e.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
- resolve(results, data);
+ onSuccess(results, data);
}
};
}
- _getAllKeysFast(objectStoreOrIndex, query, resolve, reject) {
+ _getAllKeysFast(objectStoreOrIndex, query, onSuccess, onError) {
const request = objectStoreOrIndex.getAllKeys(query);
- request.onerror = (e) => reject(e.target.error);
- request.onsuccess = (e) => resolve(e.target.result);
+ request.onerror = (e) => onError(e.target.error);
+ request.onsuccess = (e) => onSuccess(e.target.result);
}
- _getAllKeysUsingCursor(objectStoreOrIndex, query, resolve, reject) {
+ _getAllKeysUsingCursor(objectStoreOrIndex, query, onSuccess, onError) {
const results = [];
const request = objectStoreOrIndex.openKeyCursor(query, 'next');
- request.onerror = (e) => reject(e.target.error);
+ request.onerror = (e) => onError(e.target.error);
request.onsuccess = (e) => {
const cursor = e.target.result;
if (cursor) {
results.push(cursor.primaryKey);
cursor.continue();
} else {
- resolve(results);
+ onSuccess(results);
}
};
}
- _bulkDeleteInternal(objectStore, keys, onProgress, resolve, reject) {
+ _bulkDeleteInternal(objectStore, keys, onProgress) {
const count = keys.length;
- if (count === 0) {
- resolve();
- return;
- }
+ if (count === 0) { return; }
let completedCount = 0;
- const hasProgress = (typeof onProgress === 'function');
-
- const onError = (e) => reject(e.target.error);
const onSuccess = () => {
++completedCount;
- if (hasProgress) {
- try {
- onProgress(completedCount, count);
- } catch (e) {
- // NOP
- }
- }
- if (completedCount >= count) {
- resolve();
+ try {
+ onProgress(completedCount, count);
+ } catch (e) {
+ // NOP
}
};
+ const hasProgress = (typeof onProgress === 'function');
for (const key of keys) {
const request = objectStore.delete(key);
- request.onerror = onError;
- request.onsuccess = onSuccess;
+ if (hasProgress) {
+ request.onsuccess = onSuccess;
+ }
}
}
}
diff --git a/ext/js/language/dictionary-database.js b/ext/js/language/dictionary-database.js
index 62300676..6b235fb6 100644
--- a/ext/js/language/dictionary-database.js
+++ b/ext/js/language/dictionary-database.js
@@ -317,10 +317,6 @@ class DictionaryDatabase {
return this._db.bulkAdd(objectStoreName, items, start, count);
}
- persistData(objectStoreName) {
- return this._db.persistData(objectStoreName);
- }
-
// Private
_findMultiBulk(objectStoreName, indexNames, items, createQuery, predicate, createResult) {
diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js
index 8d6dcb33..89417ca6 100644
--- a/ext/js/language/dictionary-importer.js
+++ b/ext/js/language/dictionary-importer.js
@@ -149,10 +149,6 @@ class DictionaryImporter {
this._progressData.index += count;
this._progress();
}
-
- // This function is required in order to make the added data persist after the worker is terminated.
- // https://bugs.chromium.org/p/chromium/issues/detail?id=1237686
- await dictionaryDatabase.persistData(objectStoreName);
};
await bulkAdd('terms', termList);