Offload functions to worker threads with shared memory primitives for Node.js.
1import { mo } from 'moroutine';
2
3let initCount = 0;
4
5export const makeCtx = mo(import.meta, (value: string): { value: string; workerInitId: number } => {
6 initCount++;
7 return { value, workerInitId: initCount };
8});
9
10export const makeMultiplierCtx = mo(import.meta, (multiplier: number): { multiplier: number } => {
11 return { multiplier };
12});
13
14export const readCtx = mo(
15 import.meta,
16 (ctx: { value: string; workerInitId: number }): { value: string; workerInitId: number } => {
17 return ctx;
18 },
19);
20
21export const addWithCtx = mo(import.meta, (ctx: { multiplier: number }, a: number, b: number): number => {
22 return (a + b) * ctx.multiplier;
23});
24
25export const prefixValue = mo(import.meta, (ctx: { value: string; workerInitId: number }, prefix: string): string => {
26 return prefix + ctx.value;
27});