Simple proxy which runs methods in a web-worker, async and strongly-typed
-
Include workers.ts in your project
-
Make any class you wish to proxy extend
Workers.Host
, and callregisterWorker
to track.export class CalculatorWorker extends Workers.Host { add(x: number, y: number) { return x + y; } } CalculatorWorker.registerWorker();
-
Anywhere in your code call
createClient
to create a proxy:let calc = CalculatorWorker.createClient() let result = await calc.add(2, 2)
-
When you are done, call
dispose()
to shut down the workercalc.dispose();
You can use importScripts
in the class to pull in additional files your class needs, or use a module loader.
All method parameters, and return types are promises and are strongly typed.
You can use fields to maintain internal state in the class, but these are not reflected in the proxy.
The type function is:
Proxy(f(x) -> y) = f(x) -> Promise<y>
Proxy(f(x) -> Promise<y>) = f(x) -> Promise<y>