aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language/dictionary-worker.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-08-14 12:41:58 -0400
committerGitHub <noreply@github.com>2021-08-14 12:41:58 -0400
commit4839503d540adf7b48d0b1d337bef7b7c9b890ad (patch)
treec33783a36fbc48b55b5b419ab9824e66efcfa8d7 /ext/js/language/dictionary-worker.js
parent3e350bd563886e49fed309731d99f37e6f3ab320 (diff)
Threaded dictionary delete (#1895)
* Generalize dictionary worker * Add deleteDictionary functionality * Update DictionaryController to use DictionaryDatabaseModifier * Fix incorrect result handling
Diffstat (limited to 'ext/js/language/dictionary-worker.js')
-rw-r--r--ext/js/language/dictionary-worker.js22
1 files changed, 17 insertions, 5 deletions
diff --git a/ext/js/language/dictionary-worker.js b/ext/js/language/dictionary-worker.js
index 5c482178..dc546657 100644
--- a/ext/js/language/dictionary-worker.js
+++ b/ext/js/language/dictionary-worker.js
@@ -36,7 +36,10 @@ class DictionaryWorker {
const {action, params} = e.data;
switch (action) {
case 'importDictionary':
- this._onImportDictionary(params);
+ this._onMessageWithProgress(params, this._importDictionary.bind(this));
+ break;
+ case 'deleteDictionary':
+ this._onMessageWithProgress(params, this._deleteDictionary.bind(this));
break;
case 'getImageResolution.response':
this._mediaLoader.handleMessage(params);
@@ -44,7 +47,7 @@ class DictionaryWorker {
}
}
- async _onImportDictionary({details, archiveContent}) {
+ async _onMessageWithProgress(params, handler) {
const onProgress = (...args) => {
self.postMessage({
action: 'progress',
@@ -53,7 +56,7 @@ class DictionaryWorker {
};
let response;
try {
- const result = await this._importDictionary(archiveContent, details, onProgress);
+ const result = await handler(params, onProgress);
response = {result};
} catch (e) {
response = {error: serializeError(e)};
@@ -61,11 +64,11 @@ class DictionaryWorker {
self.postMessage({action: 'complete', params: response});
}
- async _importDictionary(archiveContent, importDetails, onProgress) {
+ async _importDictionary({details, archiveContent}, onProgress) {
const dictionaryDatabase = await this._getPreparedDictionaryDatabase();
try {
const dictionaryImporter = new DictionaryImporter(this._mediaLoader, onProgress);
- const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, importDetails);
+ const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, details);
return {
result,
errors: errors.map((error) => serializeError(error))
@@ -75,6 +78,15 @@ class DictionaryWorker {
}
}
+ async _deleteDictionary({dictionaryTitle}, onProgress) {
+ const dictionaryDatabase = await this._getPreparedDictionaryDatabase();
+ try {
+ return await dictionaryDatabase.deleteDictionary(dictionaryTitle, {rate: 1000}, onProgress);
+ } finally {
+ dictionaryDatabase.close();
+ }
+ }
+
async _getPreparedDictionaryDatabase() {
const dictionaryDatabase = new DictionaryDatabase();
await dictionaryDatabase.prepare();