Add transparent CultMesh TS document mutation
This commit is contained in:
parent
4189ee0811
commit
6f610a2908
2 changed files with 153 additions and 3 deletions
|
|
@ -143,6 +143,10 @@ export type CultMeshDocumentPredictionSubmitter<TDocument> = (
|
|||
value: TDocument,
|
||||
) => Promise<void>;
|
||||
|
||||
export type CultMeshDocumentUpdater<TDocument> = (
|
||||
current: TDocument,
|
||||
) => TDocument | Promise<TDocument>;
|
||||
|
||||
export type CultMeshCollectionSnapshot<TDocument> = readonly TDocument[];
|
||||
|
||||
export type CultMeshCollectionChangeKind = "added" | "updated" | "removed" | "reset";
|
||||
|
|
@ -782,6 +786,10 @@ export class CultMeshDocumentHandle<TDocument> {
|
|||
return this.submitPredictionDocument !== undefined;
|
||||
}
|
||||
|
||||
public get canSet(): boolean {
|
||||
return this.canSubmitPrediction || this.canReplace;
|
||||
}
|
||||
|
||||
public latest(context: CultMeshQueryContext | string = "local"): Promise<TDocument> {
|
||||
return this.snapshotDocument(
|
||||
this.resolveContext(typeof context === "string" ? cultMeshQueryContext(context) : context),
|
||||
|
|
@ -868,6 +876,59 @@ export class CultMeshDocumentHandle<TDocument> {
|
|||
return this.submitPredictionDocument(this.resolveContext(context), value);
|
||||
}
|
||||
|
||||
public set(value: TDocument): Promise<void>;
|
||||
public set(context: CultMeshQueryContext | string, value: TDocument): Promise<void>;
|
||||
public set(
|
||||
contextOrValue: CultMeshQueryContext | string | TDocument,
|
||||
maybeValue?: TDocument,
|
||||
): Promise<void> {
|
||||
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<TDocument>): Promise<TDocument>;
|
||||
public update(
|
||||
context: CultMeshQueryContext | string,
|
||||
update: CultMeshDocumentUpdater<TDocument>,
|
||||
): Promise<TDocument>;
|
||||
public async update(
|
||||
contextOrUpdate: CultMeshQueryContext | string | CultMeshDocumentUpdater<TDocument>,
|
||||
maybeUpdate?: CultMeshDocumentUpdater<TDocument>,
|
||||
): Promise<TDocument> {
|
||||
const hasContext =
|
||||
typeof contextOrUpdate === "string" || isCultMeshQueryContext(contextOrUpdate);
|
||||
const context = hasContext
|
||||
? contextOrUpdate as CultMeshQueryContext | string
|
||||
: "local";
|
||||
const update = hasContext ? maybeUpdate : contextOrUpdate as CultMeshDocumentUpdater<TDocument>;
|
||||
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<TAlias>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
options: { parse?: (value: unknown) => TAlias } = {},
|
||||
|
|
@ -937,6 +998,10 @@ export class CultMeshBoundDocumentHandle<TDocument> {
|
|||
return this.document.canSubmitPrediction;
|
||||
}
|
||||
|
||||
public get canSet(): boolean {
|
||||
return this.document.canSet;
|
||||
}
|
||||
|
||||
public latest(): Promise<TDocument> {
|
||||
return this.document.latest(cultMeshQueryContextFromVerse(this.verse));
|
||||
}
|
||||
|
|
@ -957,6 +1022,14 @@ export class CultMeshBoundDocumentHandle<TDocument> {
|
|||
return this.document.submitPrediction(cultMeshQueryContextFromVerse(this.verse), value);
|
||||
}
|
||||
|
||||
public set(value: TDocument): Promise<void> {
|
||||
return this.document.set(cultMeshQueryContextFromVerse(this.verse), value);
|
||||
}
|
||||
|
||||
public update(update: CultMeshDocumentUpdater<TDocument>): Promise<TDocument> {
|
||||
return this.document.update(cultMeshQueryContextFromVerse(this.verse), update);
|
||||
}
|
||||
|
||||
public asSchemaAlias<TAlias>(
|
||||
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<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
value: TDocument,
|
||||
|
|
@ -1099,6 +1176,62 @@ export class CultMeshDocumentCatalog {
|
|||
return this.document(schema, options).submitPrediction(context, value);
|
||||
}
|
||||
|
||||
public set<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
value: TDocument,
|
||||
options?: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string },
|
||||
): Promise<void>;
|
||||
public set<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
context: CultMeshQueryContext | string,
|
||||
value: TDocument,
|
||||
options?: { parse?: (value: unknown) => TDocument },
|
||||
): Promise<void>;
|
||||
public set<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
contextOrValue: CultMeshQueryContext | string | TDocument,
|
||||
valueOrOptions?: TDocument | { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string },
|
||||
maybeOptions: { parse?: (value: unknown) => TDocument } = {},
|
||||
): Promise<void> {
|
||||
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<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
update: CultMeshDocumentUpdater<TDocument>,
|
||||
options?: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string },
|
||||
): Promise<TDocument>;
|
||||
public update<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
context: CultMeshQueryContext | string,
|
||||
update: CultMeshDocumentUpdater<TDocument>,
|
||||
options?: { parse?: (value: unknown) => TDocument },
|
||||
): Promise<TDocument>;
|
||||
public update<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
contextOrUpdate: CultMeshQueryContext | string | CultMeshDocumentUpdater<TDocument>,
|
||||
updateOrOptions?: CultMeshDocumentUpdater<TDocument> | { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string },
|
||||
maybeOptions: { parse?: (value: unknown) => TDocument } = {},
|
||||
): Promise<TDocument> {
|
||||
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<TDocument>
|
||||
: contextOrUpdate as CultMeshDocumentUpdater<TDocument>;
|
||||
const options = hasContext ? maybeOptions : updateOrOptions as { parse?: (value: unknown) => TDocument } | undefined;
|
||||
return this.document(schema, options).update(context, update);
|
||||
}
|
||||
|
||||
public watch<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
callback: (value: TDocument) => void,
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
Loading…
Reference in a new issue