Type TS peer snapshot document handles
This commit is contained in:
parent
bd095a5837
commit
a0509f223b
3 changed files with 97 additions and 17 deletions
|
|
@ -288,7 +288,7 @@ await client.syncSchemaCatalog(schemas, { kinds: ["document_payload"] });
|
|||
|
||||
const health = CultMesh.documentFromPeerSnapshot(
|
||||
() => client,
|
||||
"gamecult.aetheria.daemon_health.v1",
|
||||
daemonHealthDocument,
|
||||
"daemon:aetheria.health.v1",
|
||||
{
|
||||
routeHint: CultMesh.routeHint("network", endpoint.uri),
|
||||
|
|
@ -313,7 +313,9 @@ payload binary normalization, and MessagePack decode behind the same
|
|||
`CultMeshDocumentHandle` shape as local documents. The helper prefers an exact
|
||||
schema id match and falls back to the requested record key, so runtimes that
|
||||
alias the same logical document through different generated schema names can
|
||||
still read the publication with one CultMesh call.
|
||||
still read the publication with one CultMesh call. Pass a CultCache document
|
||||
definition instead of a raw schema id when you want a typed handle whose
|
||||
`latest()` result is parsed through that definition.
|
||||
|
||||
## Streaming Mode
|
||||
|
||||
|
|
|
|||
|
|
@ -2003,6 +2003,32 @@ export function cultMeshDocumentFromPeerSnapshot(
|
|||
peer: CultNetPeer | (() => CultNetPeer | Promise<CultNetPeer>),
|
||||
schemaId: string,
|
||||
recordKey: string,
|
||||
options?: {
|
||||
documentId?: string;
|
||||
routeHint?: CultMeshRouteHint;
|
||||
sourceId?: string;
|
||||
timeoutMs?: number;
|
||||
pollMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
},
|
||||
): CultMeshDocumentHandle<unknown>;
|
||||
export function cultMeshDocumentFromPeerSnapshot<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
peer: CultNetPeer | (() => CultNetPeer | Promise<CultNetPeer>),
|
||||
definition: TDefinition,
|
||||
recordKey: string,
|
||||
options?: {
|
||||
documentId?: string;
|
||||
routeHint?: CultMeshRouteHint;
|
||||
sourceId?: string;
|
||||
timeoutMs?: number;
|
||||
pollMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
},
|
||||
): CultMeshDocumentHandle<CultCacheDocumentValue<TDefinition>>;
|
||||
export function cultMeshDocumentFromPeerSnapshot(
|
||||
peer: CultNetPeer | (() => CultNetPeer | Promise<CultNetPeer>),
|
||||
schemaOrDefinition: string | AnyCultCacheDocumentDefinition,
|
||||
recordKey: string,
|
||||
options: {
|
||||
documentId?: string;
|
||||
routeHint?: CultMeshRouteHint;
|
||||
|
|
@ -2012,29 +2038,41 @@ export function cultMeshDocumentFromPeerSnapshot(
|
|||
messageIdPrefix?: string;
|
||||
} = {},
|
||||
): CultMeshDocumentHandle<unknown> {
|
||||
requireNonEmpty(schemaId, "schemaId");
|
||||
const schema = typeof schemaOrDefinition === "string"
|
||||
? { schemaId: schemaOrDefinition }
|
||||
: cultMeshSchemaFromDefinition(schemaOrDefinition);
|
||||
const resolvedSchemaId = schema.schemaId ?? schema.type;
|
||||
if (!resolvedSchemaId) {
|
||||
throw new Error("CultMesh peer snapshot document requires a schema id or document type.");
|
||||
}
|
||||
if (typeof schemaOrDefinition === "string") {
|
||||
requireNonEmpty(schemaOrDefinition, "schemaId");
|
||||
}
|
||||
requireNonEmpty(recordKey, "recordKey");
|
||||
const documentId = options.documentId ?? recordKey;
|
||||
const read = async () =>
|
||||
decodeCultNetRawDocumentPayload(
|
||||
await requestCultNetRawSnapshotDocument(
|
||||
peer,
|
||||
schemaId,
|
||||
recordKey,
|
||||
{
|
||||
timeoutMs: options.timeoutMs,
|
||||
messageIdPrefix: options.messageIdPrefix ?? documentId,
|
||||
},
|
||||
parseCultMeshPeerSnapshotDocument(
|
||||
schemaOrDefinition,
|
||||
decodeCultNetRawDocumentPayload(
|
||||
await requestCultNetRawSnapshotDocument(
|
||||
peer,
|
||||
resolvedSchemaId,
|
||||
recordKey,
|
||||
{
|
||||
timeoutMs: options.timeoutMs,
|
||||
messageIdPrefix: options.messageIdPrefix ?? documentId,
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return cultMeshDocument(
|
||||
documentId,
|
||||
{ schemaId },
|
||||
schema,
|
||||
async () => read(),
|
||||
{
|
||||
routeHint: options.routeHint ?? cultMeshRouteHint("network", "CultNet snapshot"),
|
||||
sources: [cultMeshProjectionSource(options.sourceId ?? documentId, { schemaId })],
|
||||
sources: [cultMeshProjectionSource(options.sourceId ?? documentId, { schemaId: resolvedSchemaId })],
|
||||
watchDocument: cultMeshPollingDocumentWatcher(read, {
|
||||
intervalMs: options.pollMs ?? 250,
|
||||
}),
|
||||
|
|
@ -3754,6 +3792,32 @@ export class CultMesh {
|
|||
peer: CultNetPeer | (() => CultNetPeer | Promise<CultNetPeer>),
|
||||
schemaId: string,
|
||||
recordKey: string,
|
||||
options?: {
|
||||
documentId?: string;
|
||||
routeHint?: CultMeshRouteHint;
|
||||
sourceId?: string;
|
||||
timeoutMs?: number;
|
||||
pollMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
},
|
||||
): CultMeshDocumentHandle<unknown>;
|
||||
public static documentFromPeerSnapshot<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
peer: CultNetPeer | (() => CultNetPeer | Promise<CultNetPeer>),
|
||||
definition: TDefinition,
|
||||
recordKey: string,
|
||||
options?: {
|
||||
documentId?: string;
|
||||
routeHint?: CultMeshRouteHint;
|
||||
sourceId?: string;
|
||||
timeoutMs?: number;
|
||||
pollMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
},
|
||||
): CultMeshDocumentHandle<CultCacheDocumentValue<TDefinition>>;
|
||||
public static documentFromPeerSnapshot(
|
||||
peer: CultNetPeer | (() => CultNetPeer | Promise<CultNetPeer>),
|
||||
schemaOrDefinition: string | AnyCultCacheDocumentDefinition,
|
||||
recordKey: string,
|
||||
options: {
|
||||
documentId?: string;
|
||||
routeHint?: CultMeshRouteHint;
|
||||
|
|
@ -3763,7 +3827,9 @@ export class CultMesh {
|
|||
messageIdPrefix?: string;
|
||||
} = {},
|
||||
): CultMeshDocumentHandle<unknown> {
|
||||
return cultMeshDocumentFromPeerSnapshot(peer, schemaId, recordKey, options);
|
||||
return typeof schemaOrDefinition === "string"
|
||||
? cultMeshDocumentFromPeerSnapshot(peer, schemaOrDefinition, recordKey, options)
|
||||
: cultMeshDocumentFromPeerSnapshot(peer, schemaOrDefinition, recordKey, options);
|
||||
}
|
||||
|
||||
public static globalDocumentFromCache<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
|
|
@ -4862,6 +4928,15 @@ function decodeCultNetRawDocumentPayload(document: CultNetRawDocumentRecord): un
|
|||
return decode(toUint8Array(document.payload));
|
||||
}
|
||||
|
||||
function parseCultMeshPeerSnapshotDocument<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
schemaOrDefinition: string | TDefinition,
|
||||
value: unknown,
|
||||
): unknown | CultCacheDocumentValue<TDefinition> {
|
||||
return typeof schemaOrDefinition === "string"
|
||||
? value
|
||||
: schemaOrDefinition.schema.parse(value);
|
||||
}
|
||||
|
||||
function isCultNetErrorMessage(message: CultNetMessage): message is CultNetErrorMessage {
|
||||
return message.schemaVersion === "cultnet.error.v0";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1950,7 +1950,7 @@ test("CultMesh TS reads remote RUDP snapshots through document handles", async (
|
|||
|
||||
const document = CultMesh.documentFromPeerSnapshot(
|
||||
peer,
|
||||
"cultmesh.note.v0",
|
||||
noteDocument,
|
||||
"note:remote",
|
||||
{
|
||||
documentId: "note:remote",
|
||||
|
|
@ -1958,10 +1958,13 @@ test("CultMesh TS reads remote RUDP snapshots through document handles", async (
|
|||
},
|
||||
);
|
||||
|
||||
assert.deepEqual(await document.latest(), {
|
||||
const latest = await document.latest();
|
||||
assert.equal(latest.body, "remote handles feel local");
|
||||
assert.deepEqual(latest, {
|
||||
noteId: "note:remote",
|
||||
body: "remote handles feel local",
|
||||
});
|
||||
assert.equal(document.schema.type, "cultmesh.note");
|
||||
|
||||
const alias = CultMesh.documentFromPeerSnapshot(
|
||||
peer,
|
||||
|
|
|
|||
Loading…
Reference in a new issue