summaryrefslogtreecommitdiff
path: root/ext/js/language/dictionary-worker-handler.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/language/dictionary-worker-handler.js')
-rw-r--r--ext/js/language/dictionary-worker-handler.js40
1 files changed, 35 insertions, 5 deletions
diff --git a/ext/js/language/dictionary-worker-handler.js b/ext/js/language/dictionary-worker-handler.js
index b8c41b26..9a724386 100644
--- a/ext/js/language/dictionary-worker-handler.js
+++ b/ext/js/language/dictionary-worker-handler.js
@@ -16,24 +16,29 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-import {serializeError} from '../core.js';
+import {ExtensionError} from '../core/extension-error.js';
import {DictionaryDatabase} from './dictionary-database.js';
import {DictionaryImporter} from './dictionary-importer.js';
import {DictionaryWorkerMediaLoader} from './dictionary-worker-media-loader.js';
export class DictionaryWorkerHandler {
constructor() {
+ /** @type {DictionaryWorkerMediaLoader} */
this._mediaLoader = new DictionaryWorkerMediaLoader();
}
+ /** */
prepare() {
self.addEventListener('message', this._onMessage.bind(this), false);
}
// Private
- _onMessage(e) {
- const {action, params} = e.data;
+ /**
+ * @param {MessageEvent<import('dictionary-worker-handler').Message>} event
+ */
+ _onMessage(event) {
+ const {action, params} = event.data;
switch (action) {
case 'importDictionary':
this._onMessageWithProgress(params, this._importDictionary.bind(this));
@@ -50,7 +55,15 @@ export class DictionaryWorkerHandler {
}
}
+ /**
+ * @template [T=unknown]
+ * @param {T} params
+ * @param {(details: T, onProgress: import('dictionary-worker-handler').OnProgressCallback) => Promise<unknown>} handler
+ */
async _onMessageWithProgress(params, handler) {
+ /**
+ * @param {...unknown} args
+ */
const onProgress = (...args) => {
self.postMessage({
action: 'progress',
@@ -62,11 +75,16 @@ export class DictionaryWorkerHandler {
const result = await handler(params, onProgress);
response = {result};
} catch (e) {
- response = {error: serializeError(e)};
+ response = {error: ExtensionError.serialize(e)};
}
self.postMessage({action: 'complete', params: response});
}
+ /**
+ * @param {import('dictionary-worker-handler').ImportDictionaryMessageParams} details
+ * @param {import('dictionary-worker-handler').OnProgressCallback} onProgress
+ * @returns {Promise<import('dictionary-worker').MessageCompleteResultSerialized>}
+ */
async _importDictionary({details, archiveContent}, onProgress) {
const dictionaryDatabase = await this._getPreparedDictionaryDatabase();
try {
@@ -74,13 +92,18 @@ export class DictionaryWorkerHandler {
const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, details);
return {
result,
- errors: errors.map((error) => serializeError(error))
+ errors: errors.map((error) => ExtensionError.serialize(error))
};
} finally {
dictionaryDatabase.close();
}
}
+ /**
+ * @param {import('dictionary-worker-handler').DeleteDictionaryMessageParams} details
+ * @param {import('dictionary-database').DeleteDictionaryProgressCallback} onProgress
+ * @returns {Promise<void>}
+ */
async _deleteDictionary({dictionaryTitle}, onProgress) {
const dictionaryDatabase = await this._getPreparedDictionaryDatabase();
try {
@@ -90,6 +113,10 @@ export class DictionaryWorkerHandler {
}
}
+ /**
+ * @param {import('dictionary-worker-handler').GetDictionaryCountsMessageParams} details
+ * @returns {Promise<import('dictionary-database').DictionaryCounts>}
+ */
async _getDictionaryCounts({dictionaryNames, getTotal}) {
const dictionaryDatabase = await this._getPreparedDictionaryDatabase();
try {
@@ -99,6 +126,9 @@ export class DictionaryWorkerHandler {
}
}
+ /**
+ * @returns {Promise<DictionaryDatabase>}
+ */
async _getPreparedDictionaryDatabase() {
const dictionaryDatabase = new DictionaryDatabase();
await dictionaryDatabase.prepare();