Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738import type { Loadable } from './loadable.ts';
type TupleValues<T extends Loadable<any>[]> = { [K in keyof T]: T[K] extends Loadable<infer V> ? V : never } & {};
const SHARED = Symbol.for('moroutine.shared');
/** A fixed-length ordered list of shared-memory values with bulk `load()`/`store()` access. */export class Tuple<T extends Loadable<any>[]> implements Loadable<TupleValues<T>> { /** The individual Loadable elements, typed per index. */ readonly elements: T; readonly length: number;
constructor(elements: T) { this.elements = elements; this.length = elements.length; }
load(): TupleValues<T> { return this.elements.map((el) => el.load()) as any; }
store(values: TupleValues<T>): void { for (let i = 0; i < this.elements.length; i++) { this.elements[i].store((values as any)[i]); } }
[SHARED](): { tag: string; elements: unknown[] } { const serializedElements: unknown[] = []; for (const el of this.elements) { if (typeof el === 'object' && el !== null && SHARED in el) { serializedElements.push((el as any)[SHARED]()); } } return { tag: 'Tuple', elements: serializedElements }; }}