-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Block Bindings: Move computation of sources into dedicated useSelect
#72974
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
Conversation
| export const getBlockBindingsSourcesForBlock = createRegistrySelector( | ||
| ( select ) => | ||
| createSelector( | ||
| ( state, blockName, blockContext ) => { |
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.
The selector doesn't currently need the blockName; however, since we're memoizing it, we need it to ensure that we get a fresh list of sources if the user selects a block of a different type.
I'm planning to add logic to only return sources and source items that are compatible with the block's attributes, at which point we'll need blockName anyway.
|
Size Change: -51 B (0%) Total Size: 2.49 MB
ℹ️ View Unchanged
|
|
e2e tests are failing because some sources aren't present in the dropdown. I've debugged a bit, and it seems like Sooo we'll need to find a way to make sure that we wait for resolvers to return before reading selectors inside of a source's Fun. |
Oh, maybe it's not all that bad. 8f876bc seems to fix the issue. I hope that losing that layer of caching isn't too bad for performance. OTOH, e2e tests in |
|
I've filed a PR that removes the workaround from the e2e tests, to demonstrate that they fail in CI without the workaround: #73001 |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
6118f88 to
89d4334
Compare
This comment was marked as outdated.
This comment was marked as outdated.
ed5af86 to
72b8d56
Compare
379d683 to
837336f
Compare
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.
Pull Request Overview
This PR refactors the block bindings sources computation by moving it into a dedicated useSelect hook to fix race conditions and eliminate referential equality hacks. The changes remove workarounds from e2e tests that were previously needed to mask timing issues.
- Separates sources computation from other settings into its own
useSelecthook - Restructures the
sourcesdata structure to map source names directly to field items instead of wrapping them in objects - Removes test workarounds that manually created bindings through the UI
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
test/e2e/specs/editor/various/block-bindings/post-meta.spec.js |
Removes UI interaction workarounds and directly inserts blocks with bindings configured |
packages/block-editor/src/hooks/block-bindings.js |
Refactors sources computation into dedicated useSelect, simplifies data structure, and updates references throughout |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Jon Surrell <[email protected]>
sirreal
left a comment
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 seems to improve the problems it sets out to and I don't see cause for concern. Looks good.
I had problems the other day with tests, but it was due to an older WordPress version running in my environment.
Co-authored-by: Copilot <[email protected]>
|
We tried to get this into RC1 last week but didn't quite make it in time since we still had to sort out some problems. |
#72974) There are a few hacks and workarounds in Block Bindings to make them work. One example is the definition of `const _sources` outside of the following `useSelect` statement, which was done to avoid a console warning ("The `useSelect` hook returns different values when called with the same state and parameters"). Additionally, e2e tests had workarounds to make them pass in CI. This commit moves `sources` computation into a dedicated `useSelect`, applies some general refactoring, and removes e2e workarounds. Co-authored-by: ockham <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: sirreal <[email protected]>
|
I just cherry-picked this PR to the wp/6.9 branch to get it included in the next release: 7b1dc7f |
|
What does this fix other than removing hacks? Is this a regression in 6.9? RC is meant to be quite strict so changes don't introduce new bugs. |
|
This fixes a race condition that was visible when running e2e tests locally: When first opening the dropdown of available sources for a given bindable attribute, the The responsible code has probably been around longer than 6.9; however, we made some other changes during the 6.9 cycle which might have made the issue apparent. With this change, e2e tests now also pass locally (and with the workaround removed, we ensure that they would also fail in CI if the previous race condition reappeared). Overall, I think that that test coverage provides some confidence that the change is working fine; however, I realize that it's a fairly big change for RC2. I'd like to leave the ultimate decision with you; I'm fine if we revert the commit on the |
|
Per some additional conversation via DM, I agree that it's probably better to err on the side of caution and revert (on the 6.9 branch). I'll do that now. |
|
Reverted on the |
What?
There are a few hacks and workarounds in Block Bindings to make them work. One example is the definition of
const _sourcesoutside of the followinguseSelectstatement:gutenberg/packages/block-editor/src/hooks/block-bindings.js
Lines 300 to 305 in 2b6c500
This was done to avoid the following console warning:
However, this papered over an underlying issue (through forced referential equality).
Additionally, e2e tests had workarounds to make them pass in CI:
gutenberg/test/e2e/specs/editor/various/block-bindings/post-meta.spec.js
Lines 147 to 155 in 2b6c500
gutenberg/test/e2e/specs/editor/various/block-bindings/post-meta.spec.js
Lines 183 to 191 in 2b6c500
However, those tests (and the five in the
Fields list dropdowngroup) never passed when run locally. In other words, the workaround papered over a race condition in CI (but couldn't prevent tests from failing locally).See #65604 for a longer discussion of this issue, and #72367 (comment) for the rationale behind the present approach.
See #67885 and #72109 for further previous attempts at fixing the underlying issue.
Why?
To improve stability, remove hacks, and ensure that e2e tests actually guard against regressions.
How?
By moving
sourcescomputation into a dedicateduseSelect, and some general refactoring.Testing Instructions
See CI, but also try some smoke testing. (Refer to other Block Bindings related PRs for some ideas.)
Additionally, verify that all tests from the
post-meta.spec.jsare now passing when run locally.Follow-up
In a future iteration, we could consider introducing more granular selectors, but that can be done separately from this bugfix:
getBlockBindingsSourcesForBlockAttribute( state, blockName, blockContext, blockAttribute )selector.