mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
- 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.
16 lines
670 B
TypeScript
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() });
|
|
});
|
|
}
|