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