18 lines
743 B
TypeScript
18 lines
743 B
TypeScript
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<SavedDemandWrite>,
|
||
|
|
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));
|
||
|
|
}
|