aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/language')
-rw-r--r--ext/js/language/dictionary-database-modifier.js43
-rw-r--r--ext/js/language/dictionary-worker.js22
2 files changed, 49 insertions, 16 deletions
diff --git a/ext/js/language/dictionary-database-modifier.js b/ext/js/language/dictionary-database-modifier.js
index 3b100fae..055c4769 100644
--- a/ext/js/language/dictionary-database-modifier.js
+++ b/ext/js/language/dictionary-database-modifier.js
@@ -25,12 +25,22 @@ class DictionaryDatabaseModifier {
}
importDictionary(archiveContent, details, onProgress) {
- return this._invoke('importDictionary', {details, archiveContent}, [archiveContent], onProgress);
+ return this._invoke(
+ 'importDictionary',
+ {details, archiveContent},
+ [archiveContent],
+ onProgress,
+ this._formatimportDictionaryResult.bind(this)
+ );
+ }
+
+ deleteDictionary(dictionaryTitle, onProgress) {
+ return this._invoke('deleteDictionary', {dictionaryTitle}, [], onProgress);
}
// Private
- _invoke(action, params, transfer, onProgress) {
+ _invoke(action, params, transfer, onProgress, formatResult) {
return new Promise((resolve, reject) => {
const worker = new Worker('/js/language/dictionary-worker-main.js', {});
const details = {
@@ -39,7 +49,8 @@ class DictionaryDatabaseModifier {
resolve,
reject,
onMessage: null,
- onProgress
+ onProgress,
+ formatResult
};
const onMessage = this._onMessage.bind(this, details);
details.onMessage = onMessage;
@@ -54,15 +65,17 @@ class DictionaryDatabaseModifier {
switch (action) {
case 'complete':
{
- const {worker, resolve, reject, onMessage} = details;
+ const {worker, resolve, reject, onMessage, formatResult} = details;
details.complete = true;
details.worker = null;
details.resolve = null;
details.reject = null;
details.onMessage = null;
+ details.onProgress = null;
+ details.formatResult = null;
worker.removeEventListener('message', onMessage);
worker.terminate();
- this._onMessageComplete(params, resolve, reject);
+ this._onMessageComplete(params, resolve, reject, formatResult);
}
break;
case 'progress':
@@ -74,12 +87,21 @@ class DictionaryDatabaseModifier {
}
}
- _onMessageComplete(params, resolve, reject) {
+ _onMessageComplete(params, resolve, reject, formatResult) {
const {error} = params;
if (typeof error !== 'undefined') {
reject(deserializeError(error));
} else {
- resolve(this._formatResult(params.result));
+ let {result} = params;
+ try {
+ if (typeof formatResult === 'function') {
+ result = formatResult(result);
+ }
+ } catch (e) {
+ reject(e);
+ return;
+ }
+ resolve(result);
}
}
@@ -101,9 +123,8 @@ class DictionaryDatabaseModifier {
worker.postMessage({action: 'getImageResolution.response', params: response});
}
- _formatResult(data) {
- const {result, errors} = data;
- const errors2 = errors.map((error) => deserializeError(error));
- return {result, errors: errors2};
+ _formatimportDictionaryResult(result) {
+ result.errors = result.errors.map((error) => deserializeError(error));
+ return result;
}
}
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();