Add TS collection catalogs
This commit is contained in:
parent
964a3ff1f8
commit
258a710273
2 changed files with 154 additions and 0 deletions
|
|
@ -1627,6 +1627,87 @@ export class CultMeshCollectionHandle<TDocument> {
|
|||
}
|
||||
}
|
||||
|
||||
export class CultMeshCollectionCatalog {
|
||||
readonly #byCollectionId = new Map<string, CultMeshCollectionHandle<any>>();
|
||||
readonly #byType = new Map<string, CultMeshCollectionHandle<any>>();
|
||||
readonly #bySchemaId = new Map<string, CultMeshCollectionHandle<any>>();
|
||||
readonly #bySchemaNameVersion = new Map<string, CultMeshCollectionHandle<any>>();
|
||||
|
||||
public constructor(collections: Iterable<CultMeshCollectionHandle<any>>) {
|
||||
for (const collection of collections) {
|
||||
this.add(collection);
|
||||
}
|
||||
}
|
||||
|
||||
public get collections(): readonly CultMeshCollectionHandle<any>[] {
|
||||
return [...this.#byCollectionId.values()];
|
||||
}
|
||||
|
||||
public add<TDocument>(collection: CultMeshCollectionHandle<TDocument>): this {
|
||||
this.#byCollectionId.set(collection.collectionId, collection as CultMeshCollectionHandle<any>);
|
||||
if (collection.schema.type) {
|
||||
this.#byType.set(collection.schema.type, collection as CultMeshCollectionHandle<any>);
|
||||
}
|
||||
if (collection.schema.schemaId) {
|
||||
this.#bySchemaId.set(collection.schema.schemaId, collection as CultMeshCollectionHandle<any>);
|
||||
}
|
||||
const key = cultMeshSchemaNameVersionKey(collection.schema);
|
||||
if (key) {
|
||||
this.#bySchemaNameVersion.set(key, collection as CultMeshCollectionHandle<any>);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public tryCollection<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
options: { parse?: (value: unknown) => TDocument } = {},
|
||||
): CultMeshCollectionHandle<TDocument> | 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<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
options: { parse?: (value: unknown) => TDocument } = {},
|
||||
): CultMeshCollectionHandle<TDocument> {
|
||||
const collection = this.tryCollection(schema, options);
|
||||
if (!collection) {
|
||||
throw new Error(`Collection catalog has no collection for ${cultMeshSchemaLabel(schema)}.`);
|
||||
}
|
||||
return collection;
|
||||
}
|
||||
|
||||
public latest<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
context: CultMeshQueryContext | string = "local",
|
||||
options: { parse?: (value: unknown) => TDocument } = {},
|
||||
): Promise<CultMeshCollectionSnapshot<TDocument>> {
|
||||
return this.collection(schema, options).latest(context);
|
||||
}
|
||||
|
||||
public watchChanges<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
callback: (change: CultMeshCollectionChange<TDocument>) => void,
|
||||
options: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string } = {},
|
||||
): CultMeshUnsubscribe {
|
||||
return this.collection(schema, options).watchChanges(options.context ?? "local", callback);
|
||||
}
|
||||
}
|
||||
|
||||
export class CultMeshBoundCollectionHandle<TDocument> {
|
||||
public constructor(
|
||||
public readonly verse: CultMeshVerseContext,
|
||||
|
|
@ -2797,6 +2878,12 @@ export function cultMeshCollection<TDocument>(
|
|||
return new CultMeshCollectionHandle(collectionId, schema, snapshotCollection, options);
|
||||
}
|
||||
|
||||
export function cultMeshCollections(
|
||||
...collections: readonly CultMeshCollectionHandle<any>[]
|
||||
): CultMeshCollectionCatalog {
|
||||
return new CultMeshCollectionCatalog(collections);
|
||||
}
|
||||
|
||||
export function cultMeshCollectionFromCache<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
cache: CultCache,
|
||||
definition: TDefinition,
|
||||
|
|
@ -4756,6 +4843,12 @@ export class CultMesh {
|
|||
return cultMeshCollection(collectionId, schema, snapshotCollection, options);
|
||||
}
|
||||
|
||||
public static collections(
|
||||
...collections: readonly CultMeshCollectionHandle<any>[]
|
||||
): CultMeshCollectionCatalog {
|
||||
return cultMeshCollections(...collections);
|
||||
}
|
||||
|
||||
public static collectionFromCache<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
cache: CultCache,
|
||||
definition: TDefinition,
|
||||
|
|
|
|||
|
|
@ -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<z.infer<typeof noteAliasDocument.schema>>(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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue