summaryrefslogtreecommitdiff
path: root/ext/js/language/dictionary-worker-media-loader.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-media-loader.js
parentfd6bba8a2a869eaf2b2c1fa49001f933fce3c618 (diff)
parent23e6fb76319c9ed7c9bcdc3efba39bc5dd38f288 (diff)
Merge pull request #339 from toasted-nutbread/type-annotations
Type annotations
Diffstat (limited to 'ext/js/language/dictionary-worker-media-loader.js')
-rw-r--r--ext/js/language/dictionary-worker-media-loader.js19
1 files changed, 8 insertions, 11 deletions
diff --git a/ext/js/language/dictionary-worker-media-loader.js b/ext/js/language/dictionary-worker-media-loader.js
index d58e46c5..e19a13d3 100644
--- a/ext/js/language/dictionary-worker-media-loader.js
+++ b/ext/js/language/dictionary-worker-media-loader.js
@@ -16,7 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-import {deserializeError, generateId} from '../core.js';
+import {generateId} from '../core.js';
+import {ExtensionError} from '../core/extension-error.js';
/**
* Class used for loading and validating media from a worker thread
@@ -27,12 +28,13 @@ export class DictionaryWorkerMediaLoader {
* Creates a new instance of the media loader.
*/
constructor() {
+ /** @type {Map<string, {resolve: (result: import('dictionary-worker-media-loader').ImageDetails) => void, reject: (reason?: import('core').RejectionReason) => void}>} */
this._requests = new Map();
}
/**
* Handles a response message posted to the worker thread.
- * @param {{id: string, error: object|undefined, result: any|undefined}} params Details of the response.
+ * @param {import('dictionary-worker-media-loader').HandleMessageParams} params Details of the response.
*/
handleMessage(params) {
const {id} = params;
@@ -41,24 +43,19 @@ export class DictionaryWorkerMediaLoader {
this._requests.delete(id);
const {error} = params;
if (typeof error !== 'undefined') {
- request.reject(deserializeError(error));
+ request.reject(ExtensionError.deserialize(error));
} else {
request.resolve(params.result);
}
}
- /**
- * Attempts to load an image using an ArrayBuffer and a media type to return details about it.
- * @param {ArrayBuffer} content The binary content for the image, encoded as an ArrayBuffer.
- * @param {string} mediaType The media type for the image content.
- * @returns {Promise<{content: ArrayBuffer, width: number, height: number}>} Details about the requested image content.
- * @throws {Error} An error can be thrown if the image fails to load.
- */
+ /** @type {import('dictionary-importer-media-loader').GetImageDetailsFunction} */
getImageDetails(content, mediaType) {
return new Promise((resolve, reject) => {
const id = generateId(16);
this._requests.set(id, {resolve, reject});
- self.postMessage({
+ // This is executed in a Worker context, so the self needs to be force cast
+ /** @type {Worker} */ (/** @type {unknown} */ (self)).postMessage({
action: 'getImageDetails',
params: {id, content, mediaType}
}, [content]);