// surface 投影可能短暂混入无 identity 的条目;读 id 一律经此助手,避免渲染崩溃。 import type { SurfaceSnapshot } from './contracts' export function surfaceKey(x: { identity?: { surfaceId?: unknown } } | null | undefined): string | null { const id = x?.identity?.surfaceId return id == null || id === '' ? null : String(id) } /** 按 surfaceId 合并;同 id 取 revision 较大者。跳过无 identity 的脏项。 */ export function mergeSurfacesByRevision(a: SurfaceSnapshot[], b: SurfaceSnapshot[]): SurfaceSnapshot[] { const byId = new Map() for (const s of [...a, ...b]) { const id = surfaceKey(s) if (!id) continue const existing = byId.get(id) if (!existing || (s.revision ?? 0) >= (existing.revision ?? 0)) byId.set(id, s) } return [...byId.values()] }