-
Notifications
You must be signed in to change notification settings - Fork 800
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
Add wp-data-sync
package
#39891
Add wp-data-sync
package
#39891
Conversation
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available. Follow this PR Review Process:
Still unsure? Reach out in #jetpack-developers for guidance! |
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
This looks useful. I wonder if we are, to some extent, reimplementing I'd also want to make sure that this is generally useful for Jetpack and the other plugins in the repository. I think we should discuss it with other teams. |
I did explore that before coming up with this solution. The entity related selectors you are referring to, are mostly related to the core entities like post, user, taxonomy, comment etc. and then there is a special case for settings and all of those entities are hard-coded here. You cannot add your own entities.
Yeah, that is the goal. Initially I thought of using React Query but I didn't want to have an additional dependency when we can achieve our goal using what we already use, i.e. the |
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.
At a first glance, I'm not convinced we need yet another abstraction on top of @wordpress/data
and potentially @wordpress/core-data
. We should be able to achieve what this package provides by extending core APIs. Also, I think if we really need one, the ideal place for it would be in the WordPress core, and not here. After all, I don't really see anything Jetpack-specific here.
I did explore that before coming up with this solution. The entity related selectors you are referring to, are mostly related to the core entities like post, user, taxonomy, comment etc. and then there is a special case for settings and all of those entities are hard-coded here. You cannot add your own entities.
Unless I'm missing something, this isn't true. You can actually use:
registry.dispatch( coreStore ).addEntities()
to register custom entities, and @wordpress/core-data
will pick them up, where you'll be able to use all the regular features for retrieving / saving data, without the need to add a bunch of boilerplate. There are some examples in these articles:
- https://wordpress.stackexchange.com/questions/352500/get-post-from-custom-rest-endpoint-in-gutenberg
- https://wordpress.stackexchange.com/questions/381036/how-to-properly-add-custom-entities-in-gutenberg
This should cover the needs for handling a custom endpoint.
And if you need to expose a custom setting through the existing /settings
endpoint, you should be able to register that custom setting and enable it in REST, as demonstrated here. If anything isn't possible, we should consider extending the core APIs to make it possible.
On type safety / IDE autocompletion - it's possible that the dev experience might actually be suboptimal if you use the core features (since @wordpress/core-data
is not fully typed). If that's the case, I'd encourage you to consider contributing to core and improving the types - this would be of great benefit to everyone using @wordpress/data
stores and @wordpress/core-data
.
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.
This is a nice library, however, I think the core data entities already provide the same functionality. You register a new entity, and the library auto-generates all the actions, resolvers and selectors.
Namely the /wp/v2/settings
are already exposed there as the root/site
entity.
|
||
if ( fetchAfterUpdate ) { | ||
await dispatch[ `fetch${ capitalizedName }` ](); | ||
} |
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.
Well-written endpoints like wp/v2/settings
return the modified settings in the POST response. You don't need to fetchAfterUpdate
, because the data are already in the POST response. You just need to use them instead of ignoring them (i.e., not using the await apiFetch()
return value.
This should be the default behavior. fetchAfterUpdate
might make sense only for endpoints that are not written that well.
Also, for a really correct behavior you'll need two setter actions:
setSomething
is useful for merging a slice of the data into the current datareplaceSomething
replaces the data completely. It should be used byfetchSomething
and also for storing the response inupdateSomething
.
If you have only setSomething
then stale data might be stored in the state, namely the fields that were removed on the server.
Thank you @pablinos @tyxla @jsnajdr for your inputs. I have updated the dependent PR (#39904) to use
|
What about
Yes, as mentioned in my earlier review, this part can use some love. You might consider contributing some types, that would be awesome 😉 |
Again, too much boilerplate and in that case you need to keep track of
That is what I used in that PR. |
This is good feedback for the core project - would you like to try improving the core experience so there's some middleware, flag, or API to use that allows tracking the status of particular thunk-kind actions without the boilerplate? |
Yeah, that's a good idea. Will see what I can do. Thanks |
Often, when working on settings page UIs, we need to fetch the settings from and sync back to the server. This results in a lot of boilerplate code to define actions, action type constants, thunks, selectors, resolvers and reducers, which are often repeated for different sections.
This new package abstracts that boilerplate away and adds a way to simply define what data you need and where to get from, and then it gives you the required selectors, actions, resolvers, and a reducer, which you can pass to
@wordpress/data
store.Proposed changes:
wp-data-sync
createWpDataSync
which lets you avoid creating all the boilerplate code mentioned above.Usage
Step 1: Create the data sync
Step 2: Pass the data sync to the store
Step 3: Use it in your components
Type safety
All the selectors and actions returned are type safe and allow you to auto-complete suggestions
Other information:
Jetpack product discussion
Does this pull request change what data or activity we track or use?
Testing instructions: