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

[WIP] Try fresh-data within @wordpress/data #8271

Closed
wants to merge 2 commits into from
Closed

[WIP] Try fresh-data within @wordpress/data #8271

wants to merge 2 commits into from

Conversation

coderkevin
Copy link
Contributor

@coderkevin coderkevin commented Jul 29, 2018

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 to registerStore,
registerActions, etc.) And using a new withResources 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:

  • Freshness requirements set within the Component HOC.
  • No need to fetch data that has been fetched recently by another component.
  • Coordinating for the same resource which is needed by multiple components.
  • Pagination fetching which is still compatible with individual items of that page.
  • Updating of internal state from return values of mutating API calls.
  • Fetching multiple resources using a single API call (e.g. using includes param with ids).
  • Fetching multiple resources in one API call even if they're required by different components.

This mechanism still also supports the same features we know and love with @wordpress/data such as:

  • Declarative component interface via the HOC.
  • Automatic resolving of data and re-rendering after data arrives.
  • Registration of multiple flavors of data.
  • Custom selectors/mutations.

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#248

See the instructions on that PR for testing.

Types of changes

  • Adds registerApi to registry.
  • Adds withResources HOC.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@coderkevin coderkevin changed the title Try fresh-data within @wordpress/data [WIP] Try fresh-data within @wordpress/data Jul 29, 2018
coderkevin referenced this pull request in woocommerce/woocommerce-admin Jul 29, 2018
This utilizes code from `https://github.com/WordPress/gutenberg/pull/8271`
to try out using `fresh-data` within `@wordpress/data`.
coderkevin referenced this pull request in woocommerce/woocommerce-admin Aug 2, 2018
This utilizes code from `https://github.com/WordPress/gutenberg/pull/8271`
to try out using `fresh-data` within `@wordpress/data`.
This adds fresh-data functionality to @wordpress/data through a
`registerApi` function (in lieu of `registerStore`,
`registerActions`, etc.) And using a new `withResources` HOC.
This adds mutations to the `registerApi` options, and also a
`mapMutationsToProps` function to the `withResources` HOC.
@coderkevin
Copy link
Contributor Author

Closing in favor of #8754

@coderkevin coderkevin closed this Aug 9, 2018
coderkevin referenced this pull request in woocommerce/woocommerce-admin Aug 15, 2018
This utilizes code from `https://github.com/WordPress/gutenberg/pull/8271`
to try out using `fresh-data` within `@wordpress/data`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant