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.

docs(serve): adopt linear lifecycle pattern in README example

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

+17 -10
+8 -3
README.md
··· 387 387 388 388 ```ts 389 389 // main.ts 390 + import { once } from 'node:events'; 390 391 import { createServer } from 'node:net'; 391 392 import { workers, assign } from 'moroutine'; 392 393 import { serverThreads } from 'moroutine/serve'; 393 394 import { runServer } from './app-server.ts'; 394 395 395 396 const server = createServer(); 396 - server.listen(3000); 397 - process.on('SIGINT', () => server.close()); 398 397 399 398 { 400 399 await using run = workers(4); 401 400 using threads = serverThreads(run.workers, server); 402 - await run(threads.map(([w, args]) => assign(w, runServer(...args)))); 401 + const fanout = threads.map(([w, args]) => assign(w, runServer(...args))); 402 + void run(fanout); 403 + 404 + await once(server.listen(3000), 'listening'); 405 + console.log('Listening on :3000'); 406 + await once(process, 'SIGINT'); 407 + server.close(); 403 408 } 404 409 ``` 405 410
+9 -7
examples/server-threads/main.ts
··· 4 4 // Run: node examples/server-threads/main.ts 5 5 // Test: curl http://localhost:3000 6 6 7 + import { once } from 'node:events'; 7 8 import { createServer } from 'node:net'; 8 9 import { workers, assign } from '../../src/index.ts'; 9 10 import { serverThreads } from '../../src/serve/index.ts'; 10 11 import { runServer } from './server.ts'; 11 12 12 13 const server = createServer(); 13 - server.listen(3000); 14 - 15 - process.on('SIGINT', () => server.close()); 16 - process.on('SIGTERM', () => server.close()); 17 - 18 - console.log('Listening on http://localhost:3000 with 4 worker threads'); 19 14 20 15 { 16 + // Run server threads 21 17 await using run = workers(4); 22 18 using threads = serverThreads(run.workers, server); 23 - await run(threads.map(([w, args]) => assign(w, runServer(...args)))); 19 + const fanout = threads.map(([w, args]) => assign(w, runServer(...args))); 20 + void run(fanout); 21 + // Lifecycle 22 + await once(server.listen(3000), 'listening'); 23 + console.log('Listening on http://localhost:3000 with 4 worker threads'); 24 + await once(process, 'SIGINT'); 25 + server.close(); 24 26 }