Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

at e2cebd539403475e2f4e79d55ca1a84a0ce3510d 15 lines 492 B view raw
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);