import type { Demand } from './demand'; export type SavedDemandWrite = { version: number; demand: Demand }; /** Retain writes acknowledged after a GET began without dropping its other records. */ export function mergeDemandSnapshot( snapshot: Demand[], writes: Iterable, startedAtVersion: number, ): Demand[] { const records = new Map(snapshot.map(demand => [demand.id, demand])); for (const { version, demand } of writes) { if (version <= startedAtVersion) continue; const serverRecord = records.get(demand.id); if (!serverRecord || serverRecord.revision <= demand.revision) records.set(demand.id, demand); } return [...records.values()].sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)); }