From 6f610a2908804b7f5573a4c6b3202e7c98a94316 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 18:50:35 +0100 Subject: [PATCH] Add transparent CultMesh TS document mutation --- packages/cultmesh-ts/src/index.ts | 133 +++++++++++++++++++++ packages/cultmesh-ts/test/cultmesh.test.ts | 23 +++- 2 files changed, 153 insertions(+), 3 deletions(-) diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index fc427e24..5a9079e6 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -143,6 +143,10 @@ export type CultMeshDocumentPredictionSubmitter = ( value: TDocument, ) => Promise; +export type CultMeshDocumentUpdater = ( + current: TDocument, +) => TDocument | Promise; + export type CultMeshCollectionSnapshot = readonly TDocument[]; export type CultMeshCollectionChangeKind = "added" | "updated" | "removed" | "reset"; @@ -782,6 +786,10 @@ export class CultMeshDocumentHandle { return this.submitPredictionDocument !== undefined; } + public get canSet(): boolean { + return this.canSubmitPrediction || this.canReplace; + } + public latest(context: CultMeshQueryContext | string = "local"): Promise { return this.snapshotDocument( this.resolveContext(typeof context === "string" ? cultMeshQueryContext(context) : context), @@ -868,6 +876,59 @@ export class CultMeshDocumentHandle { return this.submitPredictionDocument(this.resolveContext(context), value); } + public set(value: TDocument): Promise; + public set(context: CultMeshQueryContext | string, value: TDocument): Promise; + public set( + contextOrValue: CultMeshQueryContext | string | TDocument, + maybeValue?: TDocument, + ): Promise { + const hasContext = + typeof contextOrValue === "string" || isCultMeshQueryContext(contextOrValue); + const context = hasContext + ? typeof contextOrValue === "string" + ? cultMeshQueryContext(contextOrValue) + : contextOrValue as CultMeshQueryContext + : cultMeshQueryContext("local"); + const value = hasContext ? maybeValue : contextOrValue as TDocument; + if (value === undefined) { + throw new Error(`Document '${this.documentId}' requires a value.`); + } + + const resolved = this.resolveContext(context); + if (this.submitPredictionDocument) { + return this.submitPredictionDocument(resolved, value); + } + if (this.replaceDocument) { + return this.replaceDocument(resolved, value); + } + + throw new Error(`Document '${this.documentId}' does not support mutation.`); + } + + public update(update: CultMeshDocumentUpdater): Promise; + public update( + context: CultMeshQueryContext | string, + update: CultMeshDocumentUpdater, + ): Promise; + public async update( + contextOrUpdate: CultMeshQueryContext | string | CultMeshDocumentUpdater, + maybeUpdate?: CultMeshDocumentUpdater, + ): Promise { + const hasContext = + typeof contextOrUpdate === "string" || isCultMeshQueryContext(contextOrUpdate); + const context = hasContext + ? contextOrUpdate as CultMeshQueryContext | string + : "local"; + const update = hasContext ? maybeUpdate : contextOrUpdate as CultMeshDocumentUpdater; + if (!update) { + throw new Error(`Document '${this.documentId}' requires an update function.`); + } + + const next = await update(await this.latest(context)); + await this.set(context, next); + return next; + } + public asSchemaAlias( schema: CultMeshDocumentSchemaDescriptor, options: { parse?: (value: unknown) => TAlias } = {}, @@ -937,6 +998,10 @@ export class CultMeshBoundDocumentHandle { return this.document.canSubmitPrediction; } + public get canSet(): boolean { + return this.document.canSet; + } + public latest(): Promise { return this.document.latest(cultMeshQueryContextFromVerse(this.verse)); } @@ -957,6 +1022,14 @@ export class CultMeshBoundDocumentHandle { return this.document.submitPrediction(cultMeshQueryContextFromVerse(this.verse), value); } + public set(value: TDocument): Promise { + return this.document.set(cultMeshQueryContextFromVerse(this.verse), value); + } + + public update(update: CultMeshDocumentUpdater): Promise { + return this.document.update(cultMeshQueryContextFromVerse(this.verse), update); + } + public asSchemaAlias( schema: CultMeshDocumentSchemaDescriptor, options: { parse?: (value: unknown) => TAlias } = {}, @@ -1072,6 +1145,10 @@ export class CultMeshDocumentCatalog { return this.tryDocument(schema)?.canSubmitPrediction ?? false; } + public canSet(schema: CultMeshDocumentSchemaDescriptor): boolean { + return this.tryDocument(schema)?.canSet ?? false; + } + public submitPrediction( schema: CultMeshDocumentSchemaDescriptor, value: TDocument, @@ -1099,6 +1176,62 @@ export class CultMeshDocumentCatalog { return this.document(schema, options).submitPrediction(context, value); } + public set( + schema: CultMeshDocumentSchemaDescriptor, + value: TDocument, + options?: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string }, + ): Promise; + public set( + schema: CultMeshDocumentSchemaDescriptor, + context: CultMeshQueryContext | string, + value: TDocument, + options?: { parse?: (value: unknown) => TDocument }, + ): Promise; + public set( + schema: CultMeshDocumentSchemaDescriptor, + contextOrValue: CultMeshQueryContext | string | TDocument, + valueOrOptions?: TDocument | { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string }, + maybeOptions: { parse?: (value: unknown) => TDocument } = {}, + ): Promise { + const hasContext = + typeof contextOrValue === "string" || isCultMeshQueryContext(contextOrValue); + const context = hasContext + ? contextOrValue as CultMeshQueryContext | string + : (valueOrOptions as { context?: CultMeshQueryContext | string } | undefined)?.context ?? "local"; + const value = hasContext ? valueOrOptions as TDocument : contextOrValue as TDocument; + const options = hasContext ? maybeOptions : valueOrOptions as { parse?: (value: unknown) => TDocument } | undefined; + return this.document(schema, options).set(context, value); + } + + public update( + schema: CultMeshDocumentSchemaDescriptor, + update: CultMeshDocumentUpdater, + options?: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string }, + ): Promise; + public update( + schema: CultMeshDocumentSchemaDescriptor, + context: CultMeshQueryContext | string, + update: CultMeshDocumentUpdater, + options?: { parse?: (value: unknown) => TDocument }, + ): Promise; + public update( + schema: CultMeshDocumentSchemaDescriptor, + contextOrUpdate: CultMeshQueryContext | string | CultMeshDocumentUpdater, + updateOrOptions?: CultMeshDocumentUpdater | { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string }, + maybeOptions: { parse?: (value: unknown) => TDocument } = {}, + ): Promise { + const hasContext = + typeof contextOrUpdate === "string" || isCultMeshQueryContext(contextOrUpdate); + const context = hasContext + ? contextOrUpdate as CultMeshQueryContext | string + : (updateOrOptions as { context?: CultMeshQueryContext | string } | undefined)?.context ?? "local"; + const update = hasContext + ? updateOrOptions as CultMeshDocumentUpdater + : contextOrUpdate as CultMeshDocumentUpdater; + const options = hasContext ? maybeOptions : updateOrOptions as { parse?: (value: unknown) => TDocument } | undefined; + return this.document(schema, options).update(context, update); + } + public watch( schema: CultMeshDocumentSchemaDescriptor, callback: (value: TDocument) => void, diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 2182e8b3..b44b1770 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -89,6 +89,7 @@ test("CultMesh TS document handles hide local cache plumbing behind typed reacti assert.equal(document.documentId, "cultmesh.note:note:1"); assert.equal(document.canReplace, true); assert.equal(document.canSubmitPrediction, false); + assert.equal(document.canSet, true); assert.equal((await bound.latest()).body, "initial"); await bound.replace({ @@ -105,7 +106,8 @@ test("CultMesh TS document handles hide local cache plumbing behind typed reacti const catalog = CultMesh.documents(document); assert.equal(catalog.canReplace(noteAliasDocument), true); - await catalog.replace(noteAliasDocument, { + assert.equal(catalog.canSet(noteAliasDocument), true); + await catalog.set(noteAliasDocument, { noteId: "note:1", body: "catalog-updated", }, { @@ -118,6 +120,12 @@ test("CultMesh TS document handles hide local cache plumbing behind typed reacti })).body, "catalog-updated", ); + const incremented = await bound.update(value => ({ + ...value, + body: `${value.body}:updated-through-bound-set`, + })); + assert.equal(incremented.body, "catalog-updated:updated-through-bound-set"); + assert.equal((await document.latest()).body, "catalog-updated:updated-through-bound-set"); assert.throws( () => document.asSchemaAlias({ schemaId: "cultmesh.other.v0" }), /not compatible/, @@ -150,10 +158,13 @@ test("CultMesh TS document handles submit predictions through configured authori assert.equal(document.canReplace, false); assert.equal(document.canSubmitPrediction, true); + assert.equal(document.canSet, true); assert.equal(bound.canSubmitPrediction, true); + assert.equal(bound.canSet, true); assert.equal(catalog.canSubmitPrediction(noteAliasDocument), true); + assert.equal(catalog.canSet(noteAliasDocument), true); - await catalog.submitPrediction(noteAliasDocument, "pilot-a", { + await catalog.set(noteAliasDocument, "pilot-a", { noteId: "note:prediction", body: "predicted", }, { @@ -168,13 +179,19 @@ test("CultMesh TS document handles submit predictions through configured authori parse: value => noteAliasDocument.schema.parse(value), }); assert.equal(alias.canSubmitPrediction, true); - await alias.submitPrediction({ + await alias.set({ noteId: "note:prediction", body: "alias-predicted", }); assert.deepEqual(predictions, ["predicted", "alias-predicted"]); assert.equal((await document.latest("pilot-a")).body, "alias-predicted"); + const updated = await document.update("pilot-a", value => ({ + ...value, + body: "updated-as-prediction", + })); + assert.equal(updated.body, "updated-as-prediction"); + assert.deepEqual(predictions, ["predicted", "alias-predicted", "updated-as-prediction"]); assert.throws( () => document.replace({