Files
scriptc/tests/corpus/native-worker-messages.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

16 lines
670 B
TypeScript

import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
let moduleCounter = 0;
function bump(): number { return ++moduleCounter; }
if (isMainThread) {
const worker = new Worker(new URL(import.meta.url), { workerData: { seed: 41 } });
worker.on("message", (value: unknown) => console.log(JSON.stringify(value)));
worker.on("exit", (code: number) => console.log("exit", code, "main", bump()));
worker.postMessage(1);
} else {
const port = parentPort!;
const data = workerData as { seed: number };
port.once("message", (value: unknown) => {
port.postMessage({ result: data.seed + (value as number), counter: bump() });
});
}