diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index d2d46991..0081ec32 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -1627,6 +1627,87 @@ export class CultMeshCollectionHandle { } } +export class CultMeshCollectionCatalog { + readonly #byCollectionId = new Map>(); + readonly #byType = new Map>(); + readonly #bySchemaId = new Map>(); + readonly #bySchemaNameVersion = new Map>(); + + public constructor(collections: Iterable>) { + for (const collection of collections) { + this.add(collection); + } + } + + public get collections(): readonly CultMeshCollectionHandle[] { + return [...this.#byCollectionId.values()]; + } + + public add(collection: CultMeshCollectionHandle): this { + this.#byCollectionId.set(collection.collectionId, collection as CultMeshCollectionHandle); + if (collection.schema.type) { + this.#byType.set(collection.schema.type, collection as CultMeshCollectionHandle); + } + if (collection.schema.schemaId) { + this.#bySchemaId.set(collection.schema.schemaId, collection as CultMeshCollectionHandle); + } + const key = cultMeshSchemaNameVersionKey(collection.schema); + if (key) { + this.#bySchemaNameVersion.set(key, collection as CultMeshCollectionHandle); + } + return this; + } + + public tryCollection( + schema: CultMeshDocumentSchemaDescriptor, + options: { parse?: (value: unknown) => TDocument } = {}, + ): CultMeshCollectionHandle | undefined { + const descriptor = normalizeCultMeshDocumentSchema(schema); + const exact = + (descriptor.type ? this.#byType.get(descriptor.type) : undefined) ?? + (descriptor.schemaId ? this.#bySchemaId.get(descriptor.schemaId) : undefined) ?? + (() => { + const key = cultMeshSchemaNameVersionKey(descriptor); + return key ? this.#bySchemaNameVersion.get(key) : undefined; + })(); + + if (!exact) { + return undefined; + } + + return cultMeshSchemasAreCompatible(exact.schema, descriptor) + ? exact.asSchemaAlias(descriptor, options) + : undefined; + } + + public collection( + schema: CultMeshDocumentSchemaDescriptor, + options: { parse?: (value: unknown) => TDocument } = {}, + ): CultMeshCollectionHandle { + const collection = this.tryCollection(schema, options); + if (!collection) { + throw new Error(`Collection catalog has no collection for ${cultMeshSchemaLabel(schema)}.`); + } + return collection; + } + + public latest( + schema: CultMeshDocumentSchemaDescriptor, + context: CultMeshQueryContext | string = "local", + options: { parse?: (value: unknown) => TDocument } = {}, + ): Promise> { + return this.collection(schema, options).latest(context); + } + + public watchChanges( + schema: CultMeshDocumentSchemaDescriptor, + callback: (change: CultMeshCollectionChange) => void, + options: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string } = {}, + ): CultMeshUnsubscribe { + return this.collection(schema, options).watchChanges(options.context ?? "local", callback); + } +} + export class CultMeshBoundCollectionHandle { public constructor( public readonly verse: CultMeshVerseContext, @@ -2797,6 +2878,12 @@ export function cultMeshCollection( return new CultMeshCollectionHandle(collectionId, schema, snapshotCollection, options); } +export function cultMeshCollections( + ...collections: readonly CultMeshCollectionHandle[] +): CultMeshCollectionCatalog { + return new CultMeshCollectionCatalog(collections); +} + export function cultMeshCollectionFromCache( cache: CultCache, definition: TDefinition, @@ -4756,6 +4843,12 @@ export class CultMesh { return cultMeshCollection(collectionId, schema, snapshotCollection, options); } + public static collections( + ...collections: readonly CultMeshCollectionHandle[] + ): CultMeshCollectionCatalog { + return cultMeshCollections(...collections); + } + public static collectionFromCache( cache: CultCache, definition: TDefinition, diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 91c346f0..993ef62d 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -740,9 +740,70 @@ test("CultMesh TS collection handles expose typed snapshots and reset watches", }).latest()).map(note => note.body).sort(), ["alpha", "bravo"], ); + const catalog = CultMesh.collections(collection); + assert.deepEqual( + (await catalog.latest(noteAliasDocument, "local", { + parse: value => noteAliasDocument.schema.parse(value), + })).map(note => note.body).sort(), + ["alpha", "bravo"], + ); + assert.equal( + catalog.collection({ schemaId: "cultmesh.note.v0" }).collectionId, + collection.collectionId, + ); + assert.equal(catalog.tryCollection({ schemaId: "cultmesh.missing.v1" }), undefined); assert.ok(changes.every(kind => kind === "reset")); }); +test("CultMesh TS collection catalogs resolve same-schema alias watches", async () => { + const current = [{ + noteId: "note:catalog-watch", + body: "initial", + }]; + let watcher: ((change: { kind: "updated"; value: typeof current[number] }) => void) | undefined; + const collection = CultMesh.collection( + "daemon:catalog-watch", + { + schemaId: "sha256:runtime-generated-note", + schemaName: "cultmesh.note", + schemaVersion: "cultmesh.note.v1", + }, + async () => current, + { + routeHint: CultMesh.routeHint("shared-memory", "collection catalog"), + watchCollection: (_context, callback) => { + watcher = callback as typeof watcher; + return () => { + watcher = undefined; + }; + }, + }, + ); + const catalog = CultMesh.collections(collection); + const observed: string[] = []; + const semanticAlias = { schemaId: "cultmesh.note.v1" }; + const unsubscribe = catalog.watchChanges>(semanticAlias, change => { + if (change.value) { + observed.push(change.value.body); + } + }, { + parse: value => noteAliasDocument.schema.parse(value), + }); + + watcher?.({ + kind: "updated", + value: { + noteId: "note:catalog-watch", + body: "through-catalog-alias", + }, + }); + unsubscribe(); + + assert.deepEqual(observed, ["through-catalog-alias"]); + assert.equal(watcher, undefined); + assert.equal(catalog.collection(semanticAlias).routeHint.description, "collection catalog"); +}); + test("CultMesh TS local authority leases do not trust peer cards by contact alone", () => { const peers = CultMesh.createPeerCatalog(); const leases = CultMesh.createAuthorityLeaseCatalog();