offload/offload.handler.ts
Anton Nesterov 2d559dfb31
init
2024-10-25 23:19:39 +02:00

15 lines
524 B
TypeScript

import type { WorkerResponse, WorkerRequest } from "./offload.interface";
declare var self: Worker;
type HandlerCallback<T, E> = ((data: E) => T) | ((data: E) => Awaited<T>);
export async function handler<T, E>(fn: HandlerCallback<T, E>): Promise<void> {
self.addEventListener("message", async (event) => {
const request = event.data as WorkerRequest<E>;
const result = await fn(request.params);
const response: WorkerResponse<T> = { id: request.id, value: result };
self.postMessage(response);
});
}