-
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
Conversation
// state are deeply merged as a base for the persisted value. | ||
if ( isPlainObject( initialState ) ) { | ||
initialState = merge( | ||
options.reducer( undefined, { |
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:
- Why defer to the default implementation for only the mismatch on this type, when if they were both non-objects we'd be fine to use the persisted value, regardless whether they're different types (array vs. string vs. number vs. etc)
- Are there cases where we'd want to allow some "empty" initial state, but still prefer the persisted value? Take, for example, the changed test case "should load a persisted value as initialState" in a1469ff where the reducer returns
null
as the default initial state
With 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.
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.
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.
Merge if possible when both it and the default initial state are objects
* Data: Fix inaccurate persistence plugin documentation * Data: Leave unpersisted keys intact in initial persisted state * Data: Add initialState option for namespace stores * Data: Use initialState option for persistence restore * Data: Deeply merge into persistence default value * Data: Remove outdated code comment for effecting initialState * Data: Persistence: Defer to default initial state in object-like mismatch * Data: Persistence: Revert to persisted value as preferred Merge if possible when both it and the default initial state are objects
* Data: Fix inaccurate persistence plugin documentation * Data: Leave unpersisted keys intact in initial persisted state * Data: Add initialState option for namespace stores * Data: Use initialState option for persistence restore * Data: Deeply merge into persistence default value * Data: Remove outdated code comment for effecting initialState * Data: Persistence: Defer to default initial state in object-like mismatch * Data: Persistence: Revert to persisted value as preferred Merge if possible when both it and the default initial state are objects
Fixes #9994
Addresses #13088 (comment)
This pull request seeks to improve the behavior of the persistence plugin with respect to restoring a persisted value. Specifically, a persisted value would previously have been used verbatim, without any consideration of whether only a subset of store keys were persisted, and without any consideration of additional new keys which could have been added to the default value (used often in our case for adding additional preferences).
There are a few tangential improvements which have been included here:
initialState
in theregisterStore
interface, which simplifies the persistence plugin slightly.Testing instructions:
Repeat steps to reproduce from #9994, verifying the observed issue has been resolved.
Ensure unit tests pass: