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

feat: add multi-module example showing moroutines from different modules on one worker

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+44
+26
examples/multi-module/main.ts
··· 1 + // Moroutines from different modules running on a single worker. 2 + // The worker lazily imports each module the first time a task from it is dispatched. 3 + // Requires Node v24+. 4 + // 5 + // Run: node examples/multi-module/main.ts 6 + 7 + import { workers } from '../../src/index.ts'; 8 + import { square, add } from './math.ts'; 9 + import { uppercase, repeat } from './text.ts'; 10 + 11 + { 12 + using run = workers(1); 13 + 14 + // All four moroutines from two modules run on the same single worker 15 + const results = await Promise.all([ 16 + run(square(7)), 17 + run(add(3, 4)), 18 + run(uppercase('hello')), 19 + run(repeat('ab', 3)), 20 + ]); 21 + 22 + console.log('square(7) =', results[0]); // 49 23 + console.log('add(3, 4) =', results[1]); // 7 24 + console.log("uppercase('hello') =", results[2]); // HELLO 25 + console.log("repeat('ab', 3) =", results[3]); // ababab 26 + }
+9
examples/multi-module/math.ts
··· 1 + import { mo } from '../../src/index.ts'; 2 + 3 + export const square = mo(import.meta, (n: number): number => { 4 + return n * n; 5 + }); 6 + 7 + export const add = mo(import.meta, (a: number, b: number): number => { 8 + return a + b; 9 + });
+9
examples/multi-module/text.ts
··· 1 + import { mo } from '../../src/index.ts'; 2 + 3 + export const uppercase = mo(import.meta, (s: string): string => { 4 + return s.toUpperCase(); 5 + }); 6 + 7 + export const repeat = mo(import.meta, (s: string, n: number): string => { 8 + return s.repeat(n); 9 + });