Skip to content
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 8 commits into from
Feb 19, 2019

Conversation

aduth
Copy link
Member

@aduth aduth commented Feb 19, 2019

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:

  • A couple corrections / improvements to the persistence plugin README
  • Implement support for initialState in the registerStore 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:

npm run test-unit packages/data

@aduth aduth added [Type] Bug An existing feature does not function as intended [Package] Data /packages/data labels Feb 19, 2019
@aduth aduth mentioned this pull request Feb 19, 2019
5 tasks
@aduth aduth requested a review from oandregal February 19, 2019 17:06
// state are deeply merged as a base for the persisted value.
if ( isPlainObject( initialState ) ) {
initialState = merge(
options.reducer( undefined, {
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

@aduth aduth merged commit a82c257 into master Feb 19, 2019
@aduth aduth deleted the fix/9994-persistence-defaults branch February 19, 2019 19:46
@youknowriad youknowriad added this to the 5.2 (Gutenberg) milestone Mar 4, 2019
youknowriad pushed a commit that referenced this pull request Mar 6, 2019
* 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
youknowriad pushed a commit that referenced this pull request Mar 6, 2019
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Data /packages/data [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Persistence data plugin should take into account the object shape
2 participants