Offload functions to worker threads with shared memory primitives for Node.js.
1import { mo } from '../../src/index.ts';
2
3// Each worker thread loads this module and gets its own `counts` map.
4// Affinity routing keeps all operations on a given key on the same worker
5// so the count is consistent.
6const counts = new Map<string, number>();
7
8export const increment = mo(import.meta, (key: string, n: number): number => {
9 const next = (counts.get(key) ?? 0) + n;
10 counts.set(key, next);
11 return next;
12});
13
14export const read = mo(import.meta, (key: string): number => {
15 return counts.get(key) ?? 0;
16});