mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
Track readable content revisions and durable read cursors in the hub. Acknowledge only rendered foreground tails, persist offline read operations, and preserve newer manual unread choices across devices. Migrate existing history to a read baseline and cover pagination receipts with shared native conformance fixtures.
37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
/** Shared by every client in a session's namespace. Versions are hub-owned. */
|
|
export const SessionReadStateSchema = z.object({
|
|
readRevision: z.number().int().nonnegative().safe(),
|
|
manualUnread: z.boolean(),
|
|
version: z.number().int().nonnegative().safe()
|
|
})
|
|
|
|
export type SessionReadState = z.infer<typeof SessionReadStateSchema>
|
|
|
|
export const SessionReadStateRequestSchema = z.discriminatedUnion('type', [
|
|
z.object({
|
|
type: z.literal('read'),
|
|
readRevision: z.number().int().nonnegative().safe(),
|
|
expectedVersion: z.number().int().nonnegative().safe()
|
|
}).strict(),
|
|
z.object({
|
|
type: z.literal('unread'),
|
|
expectedVersion: z.number().int().nonnegative().safe()
|
|
}).strict()
|
|
])
|
|
|
|
export type SessionReadStateRequest = z.infer<typeof SessionReadStateRequestSchema>
|
|
|
|
export const EMPTY_READ_STATE: SessionReadState = { readRevision: 0, manualUnread: false, version: 0 }
|
|
|
|
/** Full responses and both SSE subscriptions may arrive out of order. */
|
|
export function mergeReadState(current: SessionReadState | undefined, incoming: SessionReadState | undefined): SessionReadState | undefined {
|
|
if (!incoming) return current
|
|
if (!current) return incoming
|
|
return {
|
|
...(incoming.version > current.version ? incoming : current),
|
|
readRevision: Math.max(current.readRevision, incoming.readRevision)
|
|
}
|
|
}
|