Repository navigation
perf(flow-chat): narrow live mailbox subscription to prevent unnecessary permission UI re-renders - #3828
Conversation
…permissionrequests-01
a9f8f56 to
df9ca67
Compare
|
Thanks for working on this optimization. I’m closing this PR because the current change does not demonstrate a performance benefit.
A React/jsdom comparison of the base and PR versions showed unchanged render counts, while subscription callbacks increased from 1 to 2 and snapshot reads from 4 to 7 per permission update. A follow-up would be welcome with a reproducible performance case, session-scoped subscription improvements, and regression tests demonstrating fewer renders while preserving permission updates and device switching. |
Problem
In
usePermissionRequests, the hook previously subscribed to the wholeliveSessionInteractionStoresnapshot via a coarseuseSyncExternalStore:Because
liveSessionInteractionStore.getActiveSnapshot()yields a new object whenever any unrelated mailbox field churns (such as draft text updates, reading anchor movements, or resolution flags in other sessions),Object.iscomparison failed on every store publication. This forcedusePermissionRequestsconsumers to repeatedly re-render and recompute permission projections even whensurfaceIdandrequestswere completely unchanged.Solution
Narrowed the external store subscriptions into two focused, zero-argument slice projections:
getLiveSurfaceId: extracts onlysurfaceId.getLiveRequests: extracts onlyrequests.Both slices are derived directly from the store's immutable state and subscribed independently via
useSyncExternalStore. React now bails out re-renders viaObject.iswhenever unrelated mailbox fields update, while strictly preserving snapshot immutability (no in-place mutations or fragile ad-hoc caches).Verification
pnpm --filter @openbitfun/web-ui test -- usePermissionRequests(7/7 tests passed).node scripts/check-repo-hygiene.mjspassed.