From e905aa7a0c965a82055daa24a9a6e3bfc95655af Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 16:49:43 +0100 Subject: [PATCH] Type TS store document handles --- packages/cultmesh-ts/README.md | 5 + packages/cultmesh-ts/src/index.ts | 131 ++++++++++++++++++--- packages/cultmesh-ts/test/cultmesh.test.ts | 7 +- 3 files changed, 121 insertions(+), 22 deletions(-) diff --git a/packages/cultmesh-ts/README.md b/packages/cultmesh-ts/README.md index 25e84c3f..b4946d26 100644 --- a/packages/cultmesh-ts/README.md +++ b/packages/cultmesh-ts/README.md @@ -317,6 +317,11 @@ still read the publication with one CultMesh call. Pass a CultCache document definition instead of a raw schema id when you want a typed handle whose `latest()` result is parsed through that definition. +The same typed-definition overload is available for +`CultMesh.documentFromStore(...)` and `CultMesh.documentFromSingleFile(...)`, so +local schema publications can be read as typed reactive documents without +manually pulling records, decoding MessagePack, or parsing the payload. + ## Streaming Mode CultMesh streaming mode is for audio, video, tensor, and opaque byte frames that diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index b00ffaaa..8751b50e 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -1946,6 +1946,26 @@ export function cultMeshDocumentFromCache; +export function cultMeshDocumentFromStore( + store: CacheBackingStore, + definition: TDefinition, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + }, +): CultMeshDocumentHandle>; +export function cultMeshDocumentFromStore( + store: CacheBackingStore, + schemaOrDefinition: string | AnyCultCacheDocumentDefinition, options: { documentId?: string; routeHint?: CultMeshRouteHint; @@ -1953,24 +1973,33 @@ export function cultMeshDocumentFromStore( pollMs?: number; } = {}, ): CultMeshDocumentHandle { - requireNonEmpty(schemaId, "schemaId"); - const documentId = options.documentId ?? schemaId; + const schema = typeof schemaOrDefinition === "string" + ? { schemaId: schemaOrDefinition } + : cultMeshSchemaFromDefinition(schemaOrDefinition); + const resolvedSchemaId = schema.schemaId ?? schema.type; + if (!resolvedSchemaId) { + throw new Error("CultMesh store document requires a schema id or document type."); + } + if (typeof schemaOrDefinition === "string") { + requireNonEmpty(schemaOrDefinition, "schemaId"); + } + const documentId = options.documentId ?? resolvedSchemaId; const read = async () => { const records = await store.pullAll(); - const record = records.find(candidate => candidate.schemaId === schemaId); + const record = records.find(candidate => candidate.schemaId === resolvedSchemaId); if (!record) { - throw new Error(`CultMesh store document '${documentId}' did not contain schema ${schemaId}.`); + throw new Error(`CultMesh store document '${documentId}' did not contain schema ${resolvedSchemaId}.`); } - return decode(record.payload); + return parseCultMeshDocumentValue(schemaOrDefinition, decode(record.payload)); }; return cultMeshDocument( documentId, - { schemaId }, + schema, async () => read(), { routeHint: options.routeHint ?? cultMeshRouteHint("in-process", "CultCache store"), - sources: [cultMeshProjectionSource(options.sourceId ?? documentId, { schemaId })], + sources: [cultMeshProjectionSource(options.sourceId ?? documentId, { schemaId: resolvedSchemaId })], watchDocument: cultMeshPollingDocumentWatcher(read, { intervalMs: options.pollMs ?? 50, }), @@ -1981,6 +2010,26 @@ export function cultMeshDocumentFromStore( export function cultMeshDocumentFromSingleFile( path: string, schemaId: string, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + }, +): CultMeshDocumentHandle; +export function cultMeshDocumentFromSingleFile( + path: string, + definition: TDefinition, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + }, +): CultMeshDocumentHandle>; +export function cultMeshDocumentFromSingleFile( + path: string, + schemaOrDefinition: string | AnyCultCacheDocumentDefinition, options: { documentId?: string; routeHint?: CultMeshRouteHint; @@ -1989,14 +2038,14 @@ export function cultMeshDocumentFromSingleFile( } = {}, ): CultMeshDocumentHandle { requireNonEmpty(path, "path"); - return cultMeshDocumentFromStore( - new SingleFileMessagePackBackingStore(path), - schemaId, - { - ...options, - routeHint: options.routeHint ?? cultMeshRouteHint("shared-memory", path), - }, - ); + const store = new SingleFileMessagePackBackingStore(path); + const resolvedOptions = { + ...options, + routeHint: options.routeHint ?? cultMeshRouteHint("shared-memory", path), + }; + return typeof schemaOrDefinition === "string" + ? cultMeshDocumentFromStore(store, schemaOrDefinition, resolvedOptions) + : cultMeshDocumentFromStore(store, schemaOrDefinition, resolvedOptions); } export function cultMeshDocumentFromPeerSnapshot( @@ -2051,7 +2100,7 @@ export function cultMeshDocumentFromPeerSnapshot( requireNonEmpty(recordKey, "recordKey"); const documentId = options.documentId ?? recordKey; const read = async () => - parseCultMeshPeerSnapshotDocument( + parseCultMeshDocumentValue( schemaOrDefinition, decodeCultNetRawDocumentPayload( await requestCultNetRawSnapshotDocument( @@ -3765,6 +3814,26 @@ export class CultMesh { public static documentFromStore( store: CacheBackingStore, schemaId: string, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + }, + ): CultMeshDocumentHandle; + public static documentFromStore( + store: CacheBackingStore, + definition: TDefinition, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + }, + ): CultMeshDocumentHandle>; + public static documentFromStore( + store: CacheBackingStore, + schemaOrDefinition: string | AnyCultCacheDocumentDefinition, options: { documentId?: string; routeHint?: CultMeshRouteHint; @@ -3772,12 +3841,34 @@ export class CultMesh { pollMs?: number; } = {}, ): CultMeshDocumentHandle { - return cultMeshDocumentFromStore(store, schemaId, options); + return typeof schemaOrDefinition === "string" + ? cultMeshDocumentFromStore(store, schemaOrDefinition, options) + : cultMeshDocumentFromStore(store, schemaOrDefinition, options); } public static documentFromSingleFile( path: string, schemaId: string, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + }, + ): CultMeshDocumentHandle; + public static documentFromSingleFile( + path: string, + definition: TDefinition, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + }, + ): CultMeshDocumentHandle>; + public static documentFromSingleFile( + path: string, + schemaOrDefinition: string | AnyCultCacheDocumentDefinition, options: { documentId?: string; routeHint?: CultMeshRouteHint; @@ -3785,7 +3876,9 @@ export class CultMesh { pollMs?: number; } = {}, ): CultMeshDocumentHandle { - return cultMeshDocumentFromSingleFile(path, schemaId, options); + return typeof schemaOrDefinition === "string" + ? cultMeshDocumentFromSingleFile(path, schemaOrDefinition, options) + : cultMeshDocumentFromSingleFile(path, schemaOrDefinition, options); } public static documentFromPeerSnapshot( @@ -4928,7 +5021,7 @@ function decodeCultNetRawDocumentPayload(document: CultNetRawDocumentRecord): un return decode(toUint8Array(document.payload)); } -function parseCultMeshPeerSnapshotDocument( +function parseCultMeshDocumentValue( schemaOrDefinition: string | TDefinition, value: unknown, ): unknown | CultCacheDocumentValue { diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 08196d43..b80c375b 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -196,18 +196,19 @@ test("CultMesh TS document handles read schema publications from single-file sto }); await node.flush(); - const document = CultMesh.documentFromSingleFile(filePath, "cultmesh.note.v0", { + const document = CultMesh.documentFromSingleFile(filePath, noteDocument, { documentId: "daemon:cultmesh.note.latest", sourceId: "daemon:cultmesh.note.latest.v0", pollMs: 50, }); const observed: string[] = []; const unsubscribe = document.watch(value => { - observed.push(noteDocument.schema.parse(value).body); + observed.push(value.body); }); try { - assert.equal(noteDocument.schema.parse(await document.latest()).body, "published"); + assert.equal((await document.latest()).body, "published"); + assert.equal(document.schema.type, "cultmesh.note"); const republisher = await CultMesh.startNode(filePath, { documents: [noteDocument],