Files
scriptc/tests/corpus/native-worker-references.ts
Chris Tate 095b619330 Add native workers and shared memory
- Add native worker execution with isolated module state, structured messages, and lifecycle controls.
- Support shared buffers and integer atomics with coordinated runtime ownership and cancellation.
- Extend compiler, runtime-pack, differential, and compatibility coverage for native concurrency.
2026-10-07 19:07:02 -05:00

18 lines
784 B
TypeScript

import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
if (isMainThread) {
await new Promise<void>((resolve) => {
const worker = new Worker(new URL(import.meta.url));
console.log("handles", typeof worker.unref(), typeof worker.ref(), worker.threadId > 0);
worker.once("online", () => console.log("online"));
worker.once("message", (value: unknown) => console.log("message", value));
worker.once("exit", (code: number) => { console.log("exit", code, worker.threadId); resolve(); });
});
new Worker(new URL(import.meta.url), {workerData: "idle"}).unref();
console.log("unreferenced worker does not keep parent alive");
} else if (workerData === "idle") {
setInterval(() => {}, 1000);
} else {
parentPort!.postMessage(42);
}