Offload functions to worker threads with shared memory primitives for Node.js.
1import { mo } from 'moroutine';
2
3export const sumStream = mo(import.meta, async (input: AsyncIterable<number>): Promise<number> => {
4 let sum = 0;
5 for await (const n of input) {
6 sum += n;
7 }
8 return sum;
9});
10
11export const uppercaseStream = mo(import.meta, async function* (input: AsyncIterable<string>) {
12 for await (const s of input) {
13 yield s.toUpperCase();
14 }
15});