Files
archie/ui/src/lib/keep-unchanged.ts
T
sam 10d23e3e06
deploy / plan (push) Successful in 26s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 1m35s
fix(ui): compare live reloads structurally instead of serialising them (#1812)
2026-11-08 23:01:10 +11:00

42 lines
1.9 KiB
TypeScript

// Live events re-read whole lists, and most of what comes back is unchanged.
// Keeping the previous object for an unchanged value lets Vue skip it: an
// unchanged row keeps its component, and a reload that changed nothing
// triggers nothing at all.
// Structural comparison, not JSON.stringify: payloads such as step debug events
// and logs can be megabytes, and serialising them on every live reload (and
// caching the string beside each retained object) exhausted browser memory.
function sameContent(a: unknown, b: unknown): boolean {
if (a === b) return true;
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return false;
if (Array.isArray(a) !== Array.isArray(b)) return false;
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((item, i) => sameContent(item, b[i]));
}
const x = a as Record<string, unknown>;
const y = b as Record<string, unknown>;
const keys = Object.keys(x);
return keys.length === Object.keys(y).length && keys.every((k) => k in y && sameContent(x[k], y[k]));
}
/** The previous value when the incoming one has the same content. */
export function keepValue<T>(previous: T, incoming: T): T {
if (previous === incoming) return previous;
return sameContent(previous, incoming) ? previous : incoming;
}
/** The incoming rows, each replaced by its previous object when unchanged, and
* the previous array itself when nothing changed. */
export function keepRows<T extends { id: number | string }>(previous: readonly T[] | null, incoming: T[]): T[] {
if (!previous) return incoming;
const byId = new Map(previous.map((row) => [row.id, row]));
let changed = previous.length !== incoming.length;
const merged = incoming.map((row, index) => {
const old = byId.get(row.id);
const kept = old && sameContent(old, row) ? old : row;
if (kept !== previous[index]) changed = true;
return kept;
});
return changed ? merged : (previous as T[]);
}