From fb14eabaf166cc7870fc209237e3a47ec38fae33 Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Thu, 18 May 2023 00:18:22 +0100 Subject: [PATCH] chore: remove abuse of any in WorkerEventBus --- src/SDKVersion.tsx | 2 +- src/decrypt-worker/worker.ts | 5 +-- src/util/WorkerEventBus.ts | 67 +++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/SDKVersion.tsx b/src/SDKVersion.tsx index b213ddf..ce7d557 100644 --- a/src/SDKVersion.tsx +++ b/src/SDKVersion.tsx @@ -5,7 +5,7 @@ import { DECRYPTION_WORKER_ACTION_NAME } from './decrypt-worker/constants'; import usePromise from 'react-promise-suspense'; -const getSDKVersion = async () => { +const getSDKVersion = async (): Promise => { return workerClientBus.request(DECRYPTION_WORKER_ACTION_NAME.VERSION, null); }; diff --git a/src/decrypt-worker/worker.ts b/src/decrypt-worker/worker.ts index fb3f1ae..43679ea 100644 --- a/src/decrypt-worker/worker.ts +++ b/src/decrypt-worker/worker.ts @@ -9,7 +9,4 @@ const bus = new WorkerServerBus(); onmessage = bus.onmessage; bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, workerDecryptHandler); - -bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.VERSION, async () => { - return getSDKVersion(); -}); +bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.VERSION, getSDKVersion); diff --git a/src/util/WorkerEventBus.ts b/src/util/WorkerEventBus.ts index 9101ce3..8565d5d 100644 --- a/src/util/WorkerEventBus.ts +++ b/src/util/WorkerEventBus.ts @@ -1,30 +1,51 @@ import { nanoid } from 'nanoid'; -/* eslint-disable @typescript-eslint/no-explicit-any */ +type WorkerServerHandler = (payload: P) => R | Promise; + +interface SerializedError { + message: string; + stack?: string; +} + +interface WorkerClientRequestPayload

{ + id: string; + action: string; + payload: P; +} + +interface WorkerServerResponsePayload { + id: string; + result: R; + error: SerializedError; +} export class WorkerClientBus { - private idPromiseMap = new Map void, (error: Error) => void]>(); + private idPromiseMap = new Map void, (error: Error) => void]>(); constructor(private worker: Worker) { - worker.addEventListener('message', (e) => { - const { id, result, error } = e.data; - const actionPromise = this.idPromiseMap.get(id); - if (!actionPromise) { - console.error('cound not fetch worker promise for action: %s', id); - return; - } - this.idPromiseMap.delete(id); - - const [resolve, reject] = actionPromise; - if (error) { - reject(error); - } else { - resolve(result); - } - }); + worker.addEventListener('message', this.eventHandler); } - async request(actionName: T, payload: P): Promise { + eventHandler = (e: MessageEvent) => { + const { id, result, error } = e.data; + const actionPromise = this.idPromiseMap.get(id); + if (!actionPromise) { + console.error('cound not fetch worker promise for action: %s', id); + return; + } + this.idPromiseMap.delete(id); + + const [resolve, reject] = actionPromise; + if (error) { + const wrappedError = new Error(error.message, { cause: error }); + wrappedError.stack = error.stack; + reject(wrappedError); + } else { + resolve(result as never); + } + }; + + async request(actionName: T, payload: P): Promise { return new Promise((resolve, reject) => { const id = nanoid(); this.idPromiseMap.set(id, [resolve, reject]); @@ -38,13 +59,13 @@ export class WorkerClientBus { } export class WorkerServerBus { - private handlers = new Map Promise>(); + private handlers = new Map>(); - addEventHandler(actionName: string, handler: (payload: P) => Promise) { - this.handlers.set(actionName, handler); + addEventHandler(actionName: string, handler: WorkerServerHandler) { + this.handlers.set(actionName, handler as WorkerServerHandler); } - onmessage = async (e: MessageEvent) => { + onmessage = async (e: MessageEvent) => { const { id, action, payload } = e.data; const handler = this.handlers.get(action); if (!handler) {