diff --git a/src/services/worker.ts b/src/services/worker.ts index 12badf07..b824e8ef 100644 --- a/src/services/worker.ts +++ b/src/services/worker.ts @@ -16,6 +16,22 @@ type CommResult = { reject?: string; }; +/** + * Map of function names to functions. + */ +type FunctionMap = { [name: string]: Function }; + +/** + * Utility type to convert all methods in an object to async. + */ +type Async = { + [K in keyof T]: T[K] extends (...args: infer A) => Promise + ? (...args: A) => Promise + : T[K] extends (...args: infer A) => infer R + ? (...args: A) => Promise + : T[K]; +}; + /** * Export methods from a worker to the main thread. * @@ -34,7 +50,7 @@ type CommResult = { * inline: () => 'bar', * }); */ -export function exportWorker(handlers: T): T { +export function exportWorker(handlers: T): Async { self.onmessage = async ({ data }: { data: CommRequest }) => { try { // Get handler from registrations @@ -55,7 +71,7 @@ export function exportWorker(handlers: T } }; - return null as unknown as T; + return null as unknown as Async; } /**