Add context argument to registry.select()#11460
Add context argument to registry.select()#11460coderkevin wants to merge 3 commits intoWordPress:masterfrom coderkevin:add/context-to-withselect
Conversation
tofumatt
left a comment
There was a problem hiding this comment.
Looks like this requires a rebase to fix the merge conflicts, but I'm also curious why this is needed more specifically? What issue will making the change solve? Is there a particular use case/example you could provide that would use this code? I think it would help to have some extra context here. 😄
|
Rebased on current master. @tofumatt I'm trying to integrate another more advanced data system, fresh-data as a generic data store within With this change, the differences between these systems become transparent to the application developer. Without this change, I'd be relegated to implementing my own |
There was a problem hiding this comment.
Seems to work for me and I just have a few little comments, but I think this requires documentation updates before merging.
CC'ing @youknowriad and @aduth; not sure this is okay to merge at this point in the API freeze times.
This adds an optional `context` as a second argument to `registry.select()` and also the generic store's `getSelectors()` function. This enables context-mapped selectors, which helps with declarative data requirements based on things like UI components when they become mounted and unmounted.
This adds a context of { component: this } to withSelect's call to
registry.select. It does this by wrapping registry.select with an
anonymous function that fills in the context when it's called.
|
Rebased onto |
This adds documentation in the wp.data readme for the use of context. It also renames `genericStore` to `customStore` in the tests.
|
@tofumatt (and others), I've added docs and updated as requested. |
This adds temporary code for a `withSelect` function outside of `@wordpress/data` until the context PR is merged: WordPress/gutenberg#11460
* data: Add @fresh-data/framework to package.json * data: Add fresh-data and replace orders in table This PR adds fresh-data with a WooCommerce API spec to fulfill order information. It then replaces the existing selectors for the orders table with the new selectors as a proof-of-concept. * wc-api: Add temporary code for `withSelect` This adds temporary code for a `withSelect` function outside of `@wordpress/data` until the context PR is merged: WordPress/gutenberg#11460 * wc-api: Update fresh-data to 0.5.0
tofumatt
left a comment
There was a problem hiding this comment.
Really sorry this took ages to get to—it got buried in my review queue. But the changes look good; if you can rebase onto master then I think this is good to go?
| registry.registerGenericStore( 'custom-data', createCustomStore() ); | ||
| ``` | ||
|
|
||
| Note in the above example the handling of the optional `context` argument to `getSelectors()`. When using `withSelect`, this provides information about which component is requesting which data from the store. The benefit of this is the ability to keep track of which data is still currently needed by currently mounted components and which data is no longer needed. |
There was a problem hiding this comment.
The benefit of this is the ability to keep track of [...] which data is no longer needed
How would this be achieved? In the above example, the cache of componentResources will grow infinitely (which in its own right is very problematic). A component will not be removed if it's unmounted, nor is it obvious how this would be accomplished.
There was a problem hiding this comment.
The implementation I have for wc-admin removes components from the map upon unmount. The only components in the map are ones currently mounted at any given point in time.
|
Hey there, I just wanted to let you know that I've got this working with a separate |
|
@coderkevin does it mean this PR can be closed or do you plan to refresh it based on the revised solution you figured out? By the way, we are setting up some more detailed structure for reviewing PRs. Would you like to get automated pings for review for the code inside |
I'm not sure yet. I haven't yet circled back to this PR but hope to in the next week.
Sure, I'd be happy to help review anything in packages/data. Thanks! |
See related discussion #11460 (comment)
* Workflow: Add repository CODEOWNERS file * Fix duplicate names * Project: Add @swissspidy as i18n package code owner * Remove duplicated /packages/rich-text * Plugin: List individual code owners instead of core group * Adding @coderkevin See related discussion #11460 (comment) * Plugin: Add mobile (native.js) maintainers as code owners
* Workflow: Add repository CODEOWNERS file * Fix duplicate names * Project: Add @swissspidy as i18n package code owner * Remove duplicated /packages/rich-text * Plugin: List individual code owners instead of core group * Adding @coderkevin See related discussion #11460 (comment) * Plugin: Add mobile (native.js) maintainers as code owners
* Workflow: Add repository CODEOWNERS file * Fix duplicate names * Project: Add @swissspidy as i18n package code owner * Remove duplicated /packages/rich-text * Plugin: List individual code owners instead of core group * Adding @coderkevin See related discussion #11460 (comment) * Plugin: Add mobile (native.js) maintainers as code owners
|
@coderkevin are we still going to try and merge this in at some point? |
|
This PR would have to cover also newly introduced |
|
@timmyc Thanks for the reminder. I've been working on a modification to fresh-data that would no longer require the special handling needed in this PR. So hopefully this PR won't be needed after the next major version of fresh-data. |
How is this coming @coderkevin? Will this PR (or some variation of it, considering the staleness) be needed? |
|
Things have changed in the project inspiring this original pull, so it can be closed. |
Description
This adds an optional context argument to
registry.select()and then adds support forwithSelectto pass the component as a context for a select operation.Why is this needed?
This makes it possible to map select calls to specific components, which means a data system can have knowledge of which components depend on which data. This gives better options for shared data handling that can be tied to the lifecycle of a given component.
How has this been tested?
All existing tests still pass, and additional tests have been added to ensure the operation of this new, optional feature.
Types of changes
Adds an optional argument to
register.select( reducerKey, context).Adds code to
withSelectto supply a context of{ component }.Both changes are fully backwards-compatible.
Checklist: