mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-29 07:04:09 +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.
23 lines
795 B
TypeScript
23 lines
795 B
TypeScript
import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
|
|
|
|
if (isMainThread) {
|
|
const shared = new SharedArrayBuffer(4);
|
|
const worker = new Worker(new URL(import.meta.url), { workerData: shared });
|
|
worker.on("message", () => {
|
|
// The worker may already be parked, or may reach wait after cancellation.
|
|
worker.terminate().then((code: number) => console.log("terminated", code));
|
|
});
|
|
worker.on("exit", (code: number) => console.log("exit", code));
|
|
} else {
|
|
const values = new Int32Array(workerData as SharedArrayBuffer);
|
|
parentPort!.postMessage("ready");
|
|
try {
|
|
Atomics.wait(values, 0, 0, undefined);
|
|
console.log("unexpected wake");
|
|
} catch {
|
|
console.log("unexpected catch");
|
|
} finally {
|
|
console.log("unexpected finally");
|
|
}
|
|
}
|