Skip to content

workers

import { workers } from "@tynd/core/client";

Sandboxed Web Worker on its own OS thread. Task source and data stay inside the WebView process; executable code is never forwarded into the trusted Bun backend.

const w = await workers.spawn((input: number[]) =>
input.reduce((a, b) => a + b, 0)
);
  • fn must be self-contained — no closure captures, no outer references. It is sent as its source text; outside production builds, suspected outer references log a warning. A native function (Math.max) is refused.
  • fn may also be a source string ("(input) => input * 2"), which skips the check.
  • At most 64 live workers per renderer; spawn throws past that.
  • Arguments and return values must be supported by browser structured clone.

The handle also carries its numeric id.

const sum = await w.run<number, number[]>([1, 2, 3, 4, 5]); // 15

Serializes input, runs the function in the worker, returns the result. At most 4096 runs may be pending per worker; beyond that run rejects.

Force-stop the worker.

await w.terminate();

Ids of the workers spawned by this renderer that are still alive, sorted ascending. Handles are renderer-local, so ids from other windows are not listed.

const ids = await workers.list(); // [1, 2]
  • The renderer wraps a Web Worker via a blob: URL script (apps with a custom CSP need worker-src blob:, see Security). Each run sends input to that worker and returns the result without sharing globals or heap state.
  • Handles are local to the renderer. There is no Rust or backend IPC fallback.
  • Trusted backend code that needs Bun threads can import bunWorkers from @tynd/core; this separate API is intentionally not callable from a WebView.
  • Heavy JSON parsing — 50 MB JSON blob that would block the main thread.
  • Image / audio processing in pure JS — keep the UI responsive.
  • Computation without an event loop — simulation loops, particle systems.

For Rust-backed compute (hashing, CSPRNG), prefer compute — it’s faster and doesn’t round-trip through JSON.

  • Cannot import modules inside the worker function — everything it needs must be captured in the function body or serialized as input.
  • Workers run on OS threads, not thread pools. Spawning dozens is wasteful. Use a small pool + run tasks to it.
async function parallelMap<I, O>(
items: I[],
fn: (x: I) => O,
poolSize = 4,
): Promise<O[]> {
const pool = await Promise.all(
Array.from({ length: poolSize }, () => workers.spawn(fn)),
);
try {
const out = new Array<O>(items.length);
let idx = 0;
await Promise.all(pool.map(async (w) => {
while (idx < items.length) {
const i = idx++;
out[i] = await w.run(items[i]);
}
}));
return out;
} finally {
await Promise.all(pool.map((w) => w.terminate()));
}
}
  • compute — Rust-native hashing and RNG (faster for those tasks).
  • Streaming RPC — if you need to stream intermediate results.