Add CultMesh document catalog mutations
This commit is contained in:
parent
cb6301e1cf
commit
52ff632d70
5 changed files with 122 additions and 5 deletions
|
|
@ -1010,6 +1010,68 @@ export class CultMeshDocumentCatalog {
|
|||
return this.document(schema, options).latest(context);
|
||||
}
|
||||
|
||||
public canReplace(schema: CultMeshDocumentSchemaDescriptor): boolean {
|
||||
return this.tryDocument(schema)?.canReplace ?? false;
|
||||
}
|
||||
|
||||
public replace<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
value: TDocument,
|
||||
options?: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string },
|
||||
): Promise<void>;
|
||||
public replace<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
context: CultMeshQueryContext | string,
|
||||
value: TDocument,
|
||||
options?: { parse?: (value: unknown) => TDocument },
|
||||
): Promise<void>;
|
||||
public replace<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).replace(context, value);
|
||||
}
|
||||
|
||||
public canSubmitPrediction(schema: CultMeshDocumentSchemaDescriptor): boolean {
|
||||
return this.tryDocument(schema)?.canSubmitPrediction ?? false;
|
||||
}
|
||||
|
||||
public submitPrediction<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
value: TDocument,
|
||||
options?: { parse?: (value: unknown) => TDocument; context?: CultMeshQueryContext | string },
|
||||
): Promise<void>;
|
||||
public submitPrediction<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
context: CultMeshQueryContext | string,
|
||||
value: TDocument,
|
||||
options?: { parse?: (value: unknown) => TDocument },
|
||||
): Promise<void>;
|
||||
public submitPrediction<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).submitPrediction(context, value);
|
||||
}
|
||||
|
||||
public watch<TDocument>(
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
callback: (value: TDocument) => void,
|
||||
|
|
|
|||
|
|
@ -104,11 +104,19 @@ test("CultMesh TS document handles hide local cache plumbing behind typed reacti
|
|||
assert.equal((await alias.latest()).body, "updated");
|
||||
|
||||
const catalog = CultMesh.documents(document);
|
||||
assert.equal(catalog.canReplace(noteAliasDocument), true);
|
||||
await catalog.replace(noteAliasDocument, {
|
||||
noteId: "note:1",
|
||||
body: "catalog-updated",
|
||||
}, {
|
||||
parse: value => noteAliasDocument.schema.parse(value),
|
||||
context: "browser-client",
|
||||
});
|
||||
assert.equal(
|
||||
(await catalog.latest(noteAliasDocument, "browser-client", {
|
||||
parse: value => noteAliasDocument.schema.parse(value),
|
||||
})).body,
|
||||
"updated",
|
||||
"catalog-updated",
|
||||
);
|
||||
assert.throws(
|
||||
() => document.asSchemaAlias({ schemaId: "cultmesh.other.v0" }),
|
||||
|
|
@ -138,14 +146,18 @@ test("CultMesh TS document handles submit predictions through configured authori
|
|||
);
|
||||
const verse = CultMesh.verse("starbridge", "pilot-a");
|
||||
const bound = document.bind(verse);
|
||||
const catalog = CultMesh.documents(document);
|
||||
|
||||
assert.equal(document.canReplace, false);
|
||||
assert.equal(document.canSubmitPrediction, true);
|
||||
assert.equal(bound.canSubmitPrediction, true);
|
||||
assert.equal(catalog.canSubmitPrediction(noteAliasDocument), true);
|
||||
|
||||
await bound.submitPrediction({
|
||||
await catalog.submitPrediction(noteAliasDocument, "pilot-a", {
|
||||
noteId: "note:prediction",
|
||||
body: "predicted",
|
||||
}, {
|
||||
parse: value => noteAliasDocument.schema.parse(value),
|
||||
});
|
||||
|
||||
assert.deepEqual(contexts, ["pilot-a:network"]);
|
||||
|
|
|
|||
Loading…
Reference in a new issue