aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language/dictionary-worker-handler.js
diff options
context:
space:
mode:
authorDarius Jahandarie <djahandarie@gmail.com>2023-12-06 03:53:16 +0000
committerGitHub <noreply@github.com>2023-12-06 03:53:16 +0000
commitbd5bc1a5db29903bc098995cd9262c4576bf76af (patch)
treec9214189e0214480fcf6539ad1c6327aef6cbd1c /ext/js/language/dictionary-worker-handler.js
parentfd6bba8a2a869eaf2b2c1fa49001f933fce3c618 (diff)
parent23e6fb76319c9ed7c9bcdc3efba39bc5dd38f288 (diff)
Merge pull request #339 from toasted-nutbread/type-annotations
Type annotations
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();