From 2d5e6f839412985ae790da34b3f75476bcf8028c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Aug 2021 15:25:00 -0400 Subject: [PATCH] Fix data not being persisted after terminating Worker thread (#1877) --- ext/js/data/database.js | 10 ++++++++++ ext/js/language/dictionary-database.js | 4 ++++ ext/js/language/dictionary-importer.js | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/ext/js/data/database.js b/ext/js/data/database.js index a0a1804a..222e376c 100644 --- a/ext/js/data/database.js +++ b/ext/js/data/database.js @@ -198,6 +198,16 @@ 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); diff --git a/ext/js/language/dictionary-database.js b/ext/js/language/dictionary-database.js index 6b235fb6..62300676 100644 --- a/ext/js/language/dictionary-database.js +++ b/ext/js/language/dictionary-database.js @@ -317,6 +317,10 @@ 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 89417ca6..8d6dcb33 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -149,6 +149,10 @@ 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);