diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-09-05 20:00:08 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-05 20:00:08 -0400 |
commit | 97a80276f6ce5834ab1b9ad12309df4f6f2f3ec5 (patch) | |
tree | 28aa37aa95832cd51d55011ded62dfdbb649703e /ext/js/language/dictionary-database.js | |
parent | f68ad1f843607d4ba1ad216fe16305c420cee8d6 (diff) |
Database readwrite transaction updates (#1932)
* Add _readWriteTransaction helper
* Improve deleteDictionary order
* Make onProgress event optional
* Simplify progress rate parameter
Diffstat (limited to 'ext/js/language/dictionary-database.js')
-rw-r--r-- | ext/js/language/dictionary-database.js | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/ext/js/language/dictionary-database.js b/ext/js/language/dictionary-database.js index 6b235fb6..50435c51 100644 --- a/ext/js/language/dictionary-database.js +++ b/ext/js/language/dictionary-database.js @@ -139,22 +139,37 @@ class DictionaryDatabase { return result; } - async deleteDictionary(dictionaryName, progressSettings, onProgress) { - const targets = [ - ['dictionaries', 'title'], - ['kanji', 'dictionary'], - ['kanjiMeta', 'dictionary'], - ['terms', 'dictionary'], - ['termMeta', 'dictionary'], - ['tagMeta', 'dictionary'], - ['media', 'dictionary'] + async deleteDictionary(dictionaryName, progressRate, onProgress) { + if (typeof progressRate !== 'number') { + progressRate = 1; + } + if (typeof onProgress !== 'function') { + onProgress = () => {}; + } + + const targetGroups = [ + [ + ['kanji', 'dictionary'], + ['kanjiMeta', 'dictionary'], + ['terms', 'dictionary'], + ['termMeta', 'dictionary'], + ['tagMeta', 'dictionary'], + ['media', 'dictionary'] + ], + [ + ['dictionaries', 'title'] + ] ]; - const {rate} = progressSettings; + let storeCount = 0; + for (const targets of targetGroups) { + storeCount += targets.length; + } + const progressData = { count: 0, processed: 0, - storeCount: targets.length, + storeCount, storesProcesed: 0 }; @@ -167,18 +182,20 @@ class DictionaryDatabase { const onProgress2 = () => { const processed = progressData.processed + 1; progressData.processed = processed; - if ((processed % rate) === 0 || processed === progressData.count) { + if ((processed % progressRate) === 0 || processed === progressData.count) { onProgress(progressData); } }; - const promises = []; - for (const [objectStoreName, indexName] of targets) { - const query = IDBKeyRange.only(dictionaryName); - const promise = this._db.bulkDelete(objectStoreName, indexName, query, filterKeys, onProgress2); - promises.push(promise); + for (const targets of targetGroups) { + const promises = []; + for (const [objectStoreName, indexName] of targets) { + const query = IDBKeyRange.only(dictionaryName); + const promise = this._db.bulkDelete(objectStoreName, indexName, query, filterKeys, onProgress2); + promises.push(promise); + } + await Promise.all(promises); } - await Promise.all(promises); } findTermsBulk(termList, dictionaries, wildcard) { |