-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data: Fix persistence initial state merging behavior #13951
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de69dfe
Data: Fix inaccurate persistence plugin documentation
aduth 462ca1b
Data: Leave unpersisted keys intact in initial persisted state
aduth 88ea35f
Data: Add initialState option for namespace stores
aduth e77e26a
Data: Use initialState option for persistence restore
aduth ba66c85
Data: Deeply merge into persistence default value
aduth b7a0779
Data: Remove outdated code comment for effecting initialState
aduth a1469ff
Data: Persistence: Defer to default initial state in object-like mism…
aduth 87bf1d9
Data: Persistence: Revert to persisted value as preferred
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should ensure that this also returns a plain object or undefined to avoid a corrupted persisted state from being loaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be the expected behavior if we had a plain-object persisted state and a non-plain-object default initial state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a1469ff, I went with: If there's a mismatch of object-likeness between initial and persisted states, it defers always to the value produced by the original reducer.
But immediately upon pushing the change, I'm second-guessing myself, particularly for the following scenarios:
null
as the default initial stateWith consideration of these points, maybe it should be that the persisted value always "wins", and merges with the default implementation value if and only if both it and the persisted value are objects.
The merging behavior in general feels a bit odd to me, though I understand the need for it in introducing new keys to something like a preferences object over time. The only other ideas that came to mind to support this were things more explicitly defined as "upgrading" behaviors†. There's some overhead in trying to manage this, which I think the auto-merging handles reasonably well on its own.
† Things like: Handling the "persistence restore" action type from the reducer, checking the value and adding missing keys. Or defining a validation function which returns whether a persisted value should be considered valid for use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I also was thinking to defer to the reducer value and consider the persisted one as broken but on a second though reducers can return different shapes so I guess it's fine to override the state entirely with the persisted value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And should a need arise that we need to explicitly forget some persisted state, I think at that point we could consider introducing one of the other options I mentioned in my previous comment about validating or overriding the persisted input.