Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
function pollSnapshotEqual<T>(prev: T | null, next: T): boolean {
|
|
if (prev === next) return true;
|
|
if (prev === null) return false;
|
|
try {
|
|
return JSON.stringify(prev) === JSON.stringify(next);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function itemSnapshotEqual<T>(a: T, b: T): boolean {
|
|
if (a === b) return true;
|
|
try {
|
|
return JSON.stringify(a) === JSON.stringify(b);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** Skip React state updates when polled payload is unchanged. */
|
|
export function nextPollData<T>(prev: T | null, next: T): T {
|
|
return pollSnapshotEqual(prev, next) ? prev as T : next;
|
|
}
|
|
|
|
/**
|
|
* Merge polled list rows by key, reusing previous item references when unchanged.
|
|
* Keeps React.memo row components from re-rendering when other rows update.
|
|
*/
|
|
export function mergePollListByKey<T, K extends string | number>(
|
|
prev: T[] | null,
|
|
next: T[],
|
|
getKey: (item: T) => K,
|
|
isEqual: (a: T, b: T) => boolean = itemSnapshotEqual,
|
|
): T[] {
|
|
if (prev === null) return next;
|
|
if (prev.length !== next.length) return next;
|
|
|
|
const nextByKey = new Map(next.map((item) => [getKey(item), item]));
|
|
if (prev.some((item) => !nextByKey.has(getKey(item)))) return next;
|
|
if (next.some((item) => !prev.some((p) => getKey(p) === getKey(item)))) return next;
|
|
|
|
let changed = false;
|
|
const merged = prev.map((oldItem) => {
|
|
const newItem = nextByKey.get(getKey(oldItem))!;
|
|
if (isEqual(oldItem, newItem)) return oldItem;
|
|
changed = true;
|
|
return newItem;
|
|
});
|
|
return changed ? merged : prev;
|
|
}
|