Add shallow equality guards to MessageBlockStore to prevent unnecessary renders - #1222
Open
nordicnode wants to merge 2 commits into
Open
Add shallow equality guards to MessageBlockStore to prevent unnecessary renders#1222nordicnode wants to merge 2 commits into
nordicnode wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add shallow equality guards to MessageBlockStore to prevent unnecessary renders
Summary
• In
cli/src/state/message-block-store.ts, added shallow equality guards tosetContextandsetCallbacksto prevent state reference churn and eliminate spurious notifications to store subscribers and context consumers.• Previously,
setContext(triggered on every layout effect in<Chat />) drafted and replacedstate.contextwith a new object reference even when all property values were identical. Because Zustand notifies store subscribers whenever a new state reference is created by Immer, this triggered re-render passes for any components and listeners consuming the store or context directly.• In
setContext, added a key-by-key shallow equality guard for merged properties.• In
setCallbacks, respected full replacement semantics by checking key set length before checking individual values, ensuring that omitted callback keys trigger replacement rather than being silently preserved.• Exported and shared a single
EMPTY_RESPONSE_ADSmodule constant betweeninitialContextandchat.tsx, ensuring stable object reference identity when ads are disabled with zero allocation.• Added unit test suite in
cli/src/state/__tests__/message-block-store.test.tsverifying that subscribers fire only on true mutations, are suppressed on identical updates, and handle omitted keys insetCallbacks.Test plan
[✓]
bun test --config=/dev/null src/state/__tests__/message-block-store.test.ts— 4 pass, 0 fail[✓]
bun run --cwd cli typecheck— 0 errors[✓] PR hygiene check passed