fix(signals): patchState does not block on error - #5184
Open
rainerhahnekamp wants to merge 1 commit into
Open
Conversation
Signals can throw an error if they are accessed. `patchState` needs to call `getState` in order to provide the current state to an updater function. This fix catches errors in the internal `getState` call and keeps the errored signals in a proxy, where accessing them throws again. That means `patchState` does not block unrelated updates, and the user can apply the normal error handling features like `try/catch`, `ErrorHandler`, or the upcoming error boundaries.
rainerhahnekamp
force-pushed
the
signals/fix/patch-state
branch
from
July 1, 2026 00:23
58807f0 to
3ae6393
Compare
rainerhahnekamp
marked this pull request as ready for review
July 1, 2026 22:57
| for (const key of Reflect.ownKeys(newState)) { | ||
| if (stateKeys.includes(key)) { | ||
| const signalKey = key as keyof State; | ||
| if (currentState[signalKey] !== newState[signalKey]) { |
Member
There was a problem hiding this comment.
Is there a reason why the equality check is removed? This will now also invoke the setter of unchanged state keys.
|
|
||
| for (const key of touchedKeys) { | ||
| if (stateKeys.includes(key as string | symbol)) { | ||
| signals[key].set(draftState[key]); |
Member
There was a problem hiding this comment.
An AI review flagged a potential regression.
Is this intended, or should we add this to the breaking changes?
it('applies linked state updates after their source updates', () => {
const numberStore = signalStoreFeature(
withState({ id: 1 }),
withLinkedState(({ id }) => ({
level: () => id() * 2,
}))
)(getInitialInnerStore());
patchState(numberStore, { level: 5, id: 2 });
// This succeeded before, but now resolves in { id: 2, level: 4 }
expect(getState(numberStore)).toEqual({ id: 2, level: 5 });
});It has to do with the order in which we set the values. Doing patchState(numberStore, { id: 2, level: 5 }); is fine.
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.
Signals can throw an error if they are accessed.
patchStateneeds to callgetStatein order to provide the current state to an updater function.This fix catches errors in the internal
getStatecall and keeps the errored signals in a proxy, where accessing them throws again.That means
patchStatedoes not block unrelated updates, and the user can apply the normal error handling features liketry/catch,ErrorHandler, or the upcoming error boundaries.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?
Closes #5183
Does this PR introduce a breaking change?