um-react/src/util/WorkerEventBus.ts

95 lines
2.5 KiB
TypeScript
Raw Normal View History

import { nanoid } from 'nanoid';
import { DecryptError } from '~/decrypt-worker/util/DecryptError';
type WorkerServerHandler<P, R> = (payload: P) => R | Promise<R>;
interface SerializedError {
message: string;
stack?: string;
code?: string;
}
interface WorkerClientRequestPayload<P = unknown> {
id: string;
action: string;
payload: P;
}
interface WorkerServerResponsePayload<R = unknown> {
id: string;
result: R;
error: SerializedError;
}
2023-05-14 23:09:38 +00:00
export class WorkerClientBus<T = string> {
private idPromiseMap = new Map<string, [(data: never) => void, (error: Error) => void]>();
constructor(private worker: Worker) {
worker.addEventListener('message', this.eventHandler);
}
eventHandler = (e: MessageEvent<WorkerServerResponsePayload>) => {
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;
Object.assign(wrappedError, { code: error.code ?? null });
reject(wrappedError);
} else {
resolve(result as never);
}
};
async request<R, P>(actionName: T, payload: P): Promise<R> {
return new Promise((resolve, reject) => {
2023-05-18 21:12:45 +00:00
const id = `request://${actionName}/${nanoid()}`;
this.idPromiseMap.set(id, [resolve, reject]);
this.worker.postMessage({
id,
action: actionName,
payload,
});
});
}
}
export class WorkerServerBus {
private handlers = new Map<string, WorkerServerHandler<unknown, unknown>>();
addEventHandler<R, P>(actionName: string, handler: WorkerServerHandler<P, R>) {
this.handlers.set(actionName, handler as WorkerServerHandler<unknown, unknown>);
}
onmessage = async (e: MessageEvent<WorkerClientRequestPayload>) => {
const { id, action, payload } = e.data;
const handler = this.handlers.get(action);
let result = null;
let error = null;
if (!handler) {
error = new Error('Handler missing for action ' + action);
} else {
try {
result = await handler(payload);
} catch (err: unknown) {
if (err instanceof DecryptError) {
error = err.toJSON();
} else {
error = err;
}
}
}
postMessage({ id, result, error });
};
}