Offload functions to worker threads with shared memory primitives for Node.js.
1import { mo } from '../../src/index.ts';
2import type { SharedStruct, Int32, Mutex } from '../../src/index.ts';
3
4type Position = SharedStruct<{ x: Int32; y: Int32 }>;
5
6export const updatePosition = mo(
7 import.meta,
8 async (mu: Mutex, pos: Position, dx: number, dy: number, steps: number): Promise<void> => {
9 for (let i = 0; i < steps; i++) {
10 using guard = await mu.lock();
11 const current = pos.load();
12 pos.store({ x: current.x + dx, y: current.y + dy });
13 }
14 },
15);