| 1 | import { parentPort, workerData } from "node:worker_threads"; |
| 2 | import { WorkerWorld } from "./workerWorld.js"; |
| 3 | |
| 4 | const sync = new Int32Array(workerData.sync); |
| 5 | const world = new WorkerWorld(workerData.workerId); |
| 6 | |
| 7 | parentPort.on("message", ({ message, port }) => |
| 8 | { |
| 9 | let result; |
| 10 | try |
| 11 | { result = world.handle(message); } |
| 12 | catch (error) |
| 13 | { |
| 14 | result = { |
| 15 | __error: { |
| 16 | message: error.message, |
| 17 | userFacing: !!error.userFacing |
| 18 | } |
| 19 | }; |
| 20 | } |
| 21 | port.postMessage(result); |
| 22 | Atomics.store(sync, 0, 1); |
| 23 | Atomics.notify(sync, 0); |
| 24 | }); |