shimAttributeSource: don't run outside the registerBlockType filter#53015
Conversation
|
Size Change: -19 B (0%) Total Size: 1.44 MB
ℹ️ View Unchanged
|
| @@ -124,26 +123,3 @@ addFilter( | |||
| 'core/editor/custom-sources-backwards-compatibility/shim-attribute-source', | |||
| shimAttributeSource | |||
There was a problem hiding this comment.
Just to confirm, there's no way registerBlockType() will be called before this filter has been added?
There was a problem hiding this comment.
registerBlockType still can and will be called before adding this filter. But now there is a point, very late in the editor initialization, where the filters are re-applied. Basically re-registering the blocks once again. That's the __experimentalReapplyBlockTypeFilters action. If some block got initially registered before all filters were added, the registration will be performed once again, with the new set of filters.
The core/blocks store has two state slices for this: state.unprocessedBlockTypes contains the raw block data with the filters unapplied, and state.blockTypes contains the filtered block types. You can regenerated the blockTypes from unprocessedBlockTypes at any time, multiple times.
There was a problem hiding this comment.
Sounds good, thanks for confirming 👍
tyxla
left a comment
There was a problem hiding this comment.
Nice find, thanks for the cleanup 🚀
When adding back-compat support for
source: 'meta', theshimAttributeSourcefunction is called in ablocks.registerBlockTypefilter, which is fine, but then it's also called statically, looping through all the blocks that were already registered. All this code was added in Feb 2020, in #20544, and it all made sense.Then, year and a half later, in Oct 2021, in #34299, @gziolo added new
__experimentalReapplyBlockTypeFiltersaction, dispatched during editor initialization, which removes the need for the static loop. It re-runs theblocks.registerBlockTypefilter, automatically catching all the early block registrations. This PR removes that static loop.Found this when working on async block loading in #51778 -- the static loop goes through all registered blocks, which doesn't work well with async loading.
How to test:
There is a e2e suite that tests for this specifically. There is a
meta-attribute-blockplugin that registers two blocks, one early and one late, and tests that they both get the `source: 'meta`` behavior right.