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.

feat: type shared() return based on schema argument

Overloaded shared() so that:
- shared(int32) returns Int32
- shared({ x: int32, y: int32 }) returns SharedStruct<{ x: Int32, y: Int32 }>
- shared([int32, int64]) returns Tuple<[Int32, Int64]>
- shared(42) returns Int32
- shared(0n) returns Int64
- shared(true) returns Bool
- shared(bytes(n)) returns Bytes
- shared(string(n)) returns SharedString

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

+25
+25
src/shared/shared.ts
··· 1 + import type { Loadable } from './loadable.ts'; 1 2 import type { Descriptor, BytesDescriptor, StringDescriptor } from './descriptors.ts'; 2 3 import { int32, int64, bool } from './descriptors.ts'; 3 4 import { Int32 } from './int32.ts'; ··· 291 292 } 292 293 } 293 294 295 + // Maps a schema value to its resolved instance type 296 + type ResolveField<T> = 297 + T extends Descriptor<infer R> ? R : 298 + T extends BytesDescriptor ? Bytes : 299 + T extends StringDescriptor ? SharedString : 300 + T extends readonly unknown[] ? ResolveTuple<T> : 301 + T extends Record<string, unknown> ? ResolveStruct<T> : 302 + T extends number ? Int32 : 303 + T extends bigint ? Int64 : 304 + T extends boolean ? Bool : 305 + never; 306 + 307 + type ResolveStruct<T extends Record<string, unknown>> = SharedStruct<{ [K in keyof T]: ResolveField<T[K]> }>; 308 + type ResolveTuple<T extends readonly unknown[]> = Tuple<ResolveTupleElements<T>>; 309 + type ResolveTupleElements<T extends readonly unknown[]> = { [K in keyof T]: ResolveField<T[K]> } & Loadable<any>[]; 310 + 311 + export function shared<T extends Descriptor<any>>(schema: T): ReturnType<T>; 312 + export function shared(schema: BytesDescriptor): Bytes; 313 + export function shared(schema: StringDescriptor): SharedString; 314 + export function shared(schema: number): Int32; 315 + export function shared(schema: bigint): Int64; 316 + export function shared(schema: boolean): Bool; 317 + export function shared<T extends readonly unknown[]>(schema: [...T]): ResolveTuple<T>; 318 + export function shared<T extends Record<string, unknown>>(schema: T): ResolveStruct<T>; 294 319 export function shared(schema: unknown): any { 295 320 if (typeof schema === 'number') { 296 321 if (!Number.isInteger(schema) || schema > 2_147_483_647 || schema < -2_147_483_648) {