[WIP] Try fresh-data within @wordpress/data #8271
Closed
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.
Note: This is a work in progress and proof-of-concept at this point. It's for discussion purposes only right now.
Description
This adds fresh-data functionality to @wordpress/data through a
registerApi
function (in addition toregisterStore
,registerActions
, etc.) And using a newwithResources
HOC.The reasoning behind this addition is to support more advanced asynchronous API data needs like for the WooCommerce API and other applications which are data-heavy.
This adds the following functionality that does not presently exist in
@wordpress/data
:includes
param with ids).This mechanism still also supports the same features we know and love with
@wordpress/data
such as:Implementation details:
This implementation lays out a mapping of connected components and their data requirements. It then reduces those requirements into an actionable list of resources to be fetched. The mapping of components is critical here, because it allows the resource requirements list to be updated in accordance to component lifecycle. For instance, when a component mounts, it requires the data it needs as referenced by its props (e.g. post #25). When it unmounts, those requirements are cleared from the system and no further fetching of that data will occur (unless there's another component requiring it too, of course). It's by this mechanism that duplicate fetches are avoided without resorting to debouncing. Only one part of the code ever fetches data, and it does it in accordance with the complete mapping of all data required for that api.
Further notes:
Through this process, I've discovered some places where
fresh-data
can be simplified without reducing functionality. These changes will also provide closer integration points to the@wordpress/data
code. I am working on these changes and intend to update this code to work with them after they are all merged.Integration:
This functionality is necessary for more complex API applications. However, I'm open to discussion about how best to integrate these changes. Let's have a discussion about it.
How has this been tested?
I created a PR in
wc-admin
which uses@wordpress/data
as a dependency and implements some WooCommerce API calls: woocommerce/woocommerce-admin#248See the instructions on that PR for testing.
Types of changes
registerApi
to registry.withResources
HOC.Checklist: