web-worker-proxy
Web worker proxy for JavaScript objects.
Installation
Install package:
npm install @dumpstate/web-worker-proxy --save
Usage
- Define and build Worker script.
import { run } from "@dumpstate/web-worker-proxy"
import { Foo } from "..."
run(async () => {
// NB construct an object to be 'run' on the Worker.
// e.g. load WASM file.
return new Foo()
})
Use your favourite build tool, to bundle the script.
- Create worker proxy.
import { ProxyType, workerProxy } from "@dumpstate/web-worker-proxy"
import { Foo } from "..."
const foo: ProxyType<Foo> = await workerProxy<Foo>("/path/to/worker/script.js")
// NB ProxyType<Foo> is a type mapping, that for all:
// - properties, turn them into `() => Promise<T>` functions,
// - functions, turn them into `(...args) => Promise<ReturnType<T>>` async functions.
foo
is a Proxy, that sends a message to a dedicated Worker and resolves appropriate response.