:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(lib): add reconcileById identity-preserving list reconciliation (#68526)
Paul D'Ambra548bf78991f7
diff --git a/frontend/src/lib/utils/objects.test.ts b/frontend/src/lib/utils/objects.test.tsindex 0af386f1eb84..730255a464c0 100644--- a/frontend/src/lib/utils/objects.test.ts+++ b/frontend/src/lib/utils/objects.test.ts@@ -1,4 +1,10 @@-import { areObjectValuesEmpty, objectClean, objectCleanWithEmpty, objectDiffShallow } from 'lib/utils/objects'+import {+ areObjectValuesEmpty,+ objectClean,+ objectCleanWithEmpty,+ objectDiffShallow,+ reconcileById,+} from 'lib/utils/objects'describe('objects utils', () => {describe('areObjectValuesEmpty()', () => {@@ -51,4 +57,50 @@ describe('objects utils', () => {})})})++ describe('reconcileById()', () => {+ type Item = { id: string; status: string; count: number }+ const makeItem = (overrides: Partial<Item> = {}): Item => ({ id: 'a', status: 'done', count: 0, ...overrides })++ it('keeps the previous reference for unchanged items and the fresh one for changed items', () => {+ // A poll returns freshly parsed objects; reconcileById is what preserves identity so+ // memoized rows only re-render on real change. If it degrades to `return next`,+ // unchanged items churn references every poll and React.memo is silently defeated.+ const prevUnchanged = makeItem({ id: 'a', count: 1 })+ const prevChanged = makeItem({ id: 'b', status: 'running' })++ const nextUnchanged = makeItem({ id: 'a', count: 1 })+ const nextChanged = makeItem({ id: 'b', status: 'done' })+ const nextNew = makeItem({ id: 'c' })++ const result = reconcileById(+ [prevUnchanged, prevChanged],+ [nextUnchanged, nextChanged, nextNew],+ (item) => item.id+ )++ expect(result[0]).toBe(prevUnchanged)+ expect(result[1]).toBe(nextChanged)+ expect(result[2]).toBe(nextNew)+ })++ it('returns the next array untouched when there is no previous list', () => {+ const next = [makeItem()]+ expect(reconcileById([], next, (item) => item.id)).toBe(next)+ })++ it('never reuses items the isReusable predicate rejects, even when deep-equal', () => {+ // An in-flight item's row may render wall-clock time: reusing its reference would let a+ // memoized row skip the poll re-render and freeze.+ const prev = makeItem({ status: 'running' })+ const next = makeItem({ status: 'running' })+ const result = reconcileById(+ [prev],+ [next],+ (item) => item.id,+ (item) => item.status !== 'running'+ )+ expect(result[0]).toBe(next)+ })+ })})diff --git a/frontend/src/lib/utils/objects.ts b/frontend/src/lib/utils/objects.tsindex 23559631f423..3956de7a1fcd 100644--- a/frontend/src/lib/utils/objects.ts+++ b/frontend/src/lib/utils/objects.ts@@ -11,6 +11,31 @@ export function objectsEqual(obj1: any, obj2: any): boolean {return equal(obj1, obj2)}+/**+ * Reuse the previous list's object reference for any item whose content is unchanged. Polled+ * endpoints return freshly parsed objects on every cycle, so without this every item's reference+ * changes each poll and every memoized row re-renders even when nothing changed. Matching by id+ * and reusing the old reference when deep-equal keeps identity stable through selectors, so+ * `React.memo` on the rows can actually bite.+ */+export function reconcileById<T>(+ previous: T[],+ next: T[],+ getId: (item: T) => string,+ // Items whose rendering depends on wall-clock time (e.g. an in-flight run's ticking duration)+ // must NOT be reused: a preserved reference lets a memoized row skip the poll's re-render and freeze.+ isReusable: (item: T) => boolean = () => true+): T[] {+ if (previous.length === 0) {+ return next+ }+ const previousById = new Map(previous.map((item) => [getId(item), item]))+ return next.map((item) => {+ const existing = previousById.get(getId(item))+ return existing && isReusable(item) && objectsEqual(existing, item) ? existing : item+ })+}+// https://stackoverflow.com/questions/25421233/javascript-removing-undefined-fields-from-an-objectexport function objectClean<T extends Record<string | number | symbol, unknown>>(obj: T,