chore: remove abuse of any in WorkerEventBus

This commit is contained in:
鲁树人 2023-05-18 00:18:22 +01:00
parent 8f071d94c6
commit 7df50c7c75
3 changed files with 46 additions and 28 deletions

View File

@ -5,7 +5,7 @@ import { DECRYPTION_WORKER_ACTION_NAME } from './decrypt-worker/constants';
import usePromise from 'react-promise-suspense'; import usePromise from 'react-promise-suspense';
const getSDKVersion = async () => { const getSDKVersion = async (): Promise<string> => {
return workerClientBus.request(DECRYPTION_WORKER_ACTION_NAME.VERSION, null); return workerClientBus.request(DECRYPTION_WORKER_ACTION_NAME.VERSION, null);
}; };

View File

@ -9,7 +9,4 @@ const bus = new WorkerServerBus();
onmessage = bus.onmessage; onmessage = bus.onmessage;
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, workerDecryptHandler); bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, workerDecryptHandler);
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.VERSION, getSDKVersion);
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.VERSION, async () => {
return getSDKVersion();
});

View File

@ -1,12 +1,32 @@
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
/* eslint-disable @typescript-eslint/no-explicit-any */ type WorkerServerHandler<P, R> = (payload: P) => R | Promise<R>;
interface SerializedError {
message: string;
stack?: string;
}
interface WorkerClientRequestPayload<P = unknown> {
id: string;
action: string;
payload: P;
}
interface WorkerServerResponsePayload<R = unknown> {
id: string;
result: R;
error: SerializedError;
}
export class WorkerClientBus<T = string> { export class WorkerClientBus<T = string> {
private idPromiseMap = new Map<string, [(data: any) => void, (error: Error) => void]>(); private idPromiseMap = new Map<string, [(data: never) => void, (error: Error) => void]>();
constructor(private worker: Worker) { constructor(private worker: Worker) {
worker.addEventListener('message', (e) => { worker.addEventListener('message', this.eventHandler);
}
eventHandler = (e: MessageEvent<WorkerServerResponsePayload>) => {
const { id, result, error } = e.data; const { id, result, error } = e.data;
const actionPromise = this.idPromiseMap.get(id); const actionPromise = this.idPromiseMap.get(id);
if (!actionPromise) { if (!actionPromise) {
@ -17,14 +37,15 @@ export class WorkerClientBus<T = string> {
const [resolve, reject] = actionPromise; const [resolve, reject] = actionPromise;
if (error) { if (error) {
reject(error); const wrappedError = new Error(error.message, { cause: error });
wrappedError.stack = error.stack;
reject(wrappedError);
} else { } else {
resolve(result); resolve(result as never);
}
});
} }
};
async request<R = any, P = any>(actionName: T, payload: P): Promise<R> { async request<R, P>(actionName: T, payload: P): Promise<R> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const id = nanoid(); const id = nanoid();
this.idPromiseMap.set(id, [resolve, reject]); this.idPromiseMap.set(id, [resolve, reject]);
@ -38,13 +59,13 @@ export class WorkerClientBus<T = string> {
} }
export class WorkerServerBus { export class WorkerServerBus {
private handlers = new Map<string, (payload: any) => Promise<any>>(); private handlers = new Map<string, WorkerServerHandler<unknown, unknown>>();
addEventHandler<R = any, P = any>(actionName: string, handler: (payload: P) => Promise<R>) { addEventHandler<R, P>(actionName: string, handler: WorkerServerHandler<P, R>) {
this.handlers.set(actionName, handler); this.handlers.set(actionName, handler as WorkerServerHandler<unknown, unknown>);
} }
onmessage = async (e: MessageEvent<any>) => { onmessage = async (e: MessageEvent<WorkerClientRequestPayload>) => {
const { id, action, payload } = e.data; const { id, action, payload } = e.data;
const handler = this.handlers.get(action); const handler = this.handlers.get(action);
if (!handler) { if (!handler) {