fix(signals): avoid reading unrelated state slices in patchState - #5212
Open
97Fakhreddine wants to merge 1 commit into
Open
fix(signals): avoid reading unrelated state slices in patchState#521297Fakhreddine wants to merge 1 commit into
97Fakhreddine wants to merge 1 commit into
Conversation
✅ Deploy Preview for ngrx-io ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
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.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
patchStatecurrently reads the complete state throughgetStatebefore applying an update.Because
getStateevaluates every state slice, patching an unrelated property can fail when another state slice throws while being read, for example a slice backed by aresourceorlinkedSignal.This means a call such as:
can be blocked by an unrelated state slice even though that slice is not part of the update.
Closes #5183
What is the new behavior?
For partial state object updates,
patchStateno longer eagerly reads the complete state.Instead, it only reads the state slices that are actually being updated when it needs to compare their current values.
Functional updaters still receive the complete current state as before:
This preserves the existing behavior of functional updaters while allowing independent state slices to be patched even when another unrelated slice cannot currently be read.
Implementation
The change distinguishes between two cases:
getState.For partial object updates, the current value is read directly from the corresponding signal before deciding whether the signal needs to be updated.
This also preserves the existing equality check so unchanged state slices are not written unnecessarily.
Tests
A regression test was added that creates:
namestate sliceThe test verifies that:
does not read the throwing slice, does not throw, and successfully updates
name.The Signals test suite passes with the change:
Does this PR introduce a breaking change?
The behavior of functional updaters remains unchanged. The change only prevents unnecessary reads of unrelated state slices when using partial state object updates.