From e7eb60a1176c923a2e20ce025e7531a0ccb1ed21 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Wed, 22 May 2024 13:10:53 +0200 Subject: [PATCH] withRegistryProvider: prevent intermediate state with no children (#61859) Co-authored-by: jsnajdr Co-authored-by: youknowriad Co-authored-by: tyxla Co-authored-by: annezazu --- .../provider/with-registry-provider.js | 71 +++++++++---------- .../provider/with-registry-provider.js | 63 ++++++++-------- 2 files changed, 65 insertions(+), 69 deletions(-) diff --git a/packages/block-editor/src/components/provider/with-registry-provider.js b/packages/block-editor/src/components/provider/with-registry-provider.js index 5c00da6e93eabc..582a9bd547f20e 100644 --- a/packages/block-editor/src/components/provider/with-registry-provider.js +++ b/packages/block-editor/src/components/provider/with-registry-provider.js @@ -1,12 +1,8 @@ /** * WordPress dependencies */ -import { useState, useEffect } from '@wordpress/element'; -import { - withRegistry, - createRegistry, - RegistryProvider, -} from '@wordpress/data'; +import { useState } from '@wordpress/element'; +import { useRegistry, createRegistry, RegistryProvider } from '@wordpress/data'; import { createHigherOrderComponent } from '@wordpress/compose'; /** @@ -15,41 +11,40 @@ import { createHigherOrderComponent } from '@wordpress/compose'; import { storeConfig } from '../../store'; import { STORE_NAME as blockEditorStoreName } from '../../store/constants'; -const withRegistryProvider = createHigherOrderComponent( - ( WrappedComponent ) => { - return withRegistry( - ( { useSubRegistry = true, registry, ...props } ) => { - if ( ! useSubRegistry ) { - return ( - - ); - } - - const [ subRegistry, setSubRegistry ] = useState( null ); - useEffect( () => { - const newRegistry = createRegistry( {}, registry ); - newRegistry.registerStore( - blockEditorStoreName, - storeConfig - ); - setSubRegistry( newRegistry ); - }, [ registry ] ); +function getSubRegistry( subRegistries, registry, useSubRegistry ) { + if ( ! useSubRegistry ) { + return registry; + } + let subRegistry = subRegistries.get( registry ); + if ( ! subRegistry ) { + subRegistry = createRegistry( {}, registry ); + subRegistry.registerStore( blockEditorStoreName, storeConfig ); + subRegistries.set( registry, subRegistry ); + } + return subRegistry; +} - if ( ! subRegistry ) { - return null; - } +const withRegistryProvider = createHigherOrderComponent( + ( WrappedComponent ) => + ( { useSubRegistry = true, ...props } ) => { + const registry = useRegistry(); + const [ subRegistries ] = useState( () => new WeakMap() ); + const subRegistry = getSubRegistry( + subRegistries, + registry, + useSubRegistry + ); - return ( - - - - ); + if ( subRegistry === registry ) { + return ; } - ); - }, + + return ( + + + + ); + }, 'withRegistryProvider' ); diff --git a/packages/editor/src/components/provider/with-registry-provider.js b/packages/editor/src/components/provider/with-registry-provider.js index 32c30a9f8d6873..cebbb766ff9429 100644 --- a/packages/editor/src/components/provider/with-registry-provider.js +++ b/packages/editor/src/components/provider/with-registry-provider.js @@ -1,12 +1,8 @@ /** * WordPress dependencies */ -import { useState, useEffect } from '@wordpress/element'; -import { - withRegistry, - createRegistry, - RegistryProvider, -} from '@wordpress/data'; +import { useState } from '@wordpress/element'; +import { useRegistry, createRegistry, RegistryProvider } from '@wordpress/data'; import { createHigherOrderComponent } from '@wordpress/compose'; import { storeConfig as blockEditorStoreConfig } from '@wordpress/block-editor'; @@ -15,41 +11,46 @@ import { storeConfig as blockEditorStoreConfig } from '@wordpress/block-editor'; */ import { storeConfig } from '../../store'; +function getSubRegistry( subRegistries, registry, useSubRegistry ) { + if ( ! useSubRegistry ) { + return registry; + } + let subRegistry = subRegistries.get( registry ); + if ( ! subRegistry ) { + subRegistry = createRegistry( + { + 'core/block-editor': blockEditorStoreConfig, + }, + registry + ); + // Todo: The interface store should also be created per instance. + subRegistry.registerStore( 'core/editor', storeConfig ); + subRegistries.set( registry, subRegistry ); + } + return subRegistry; +} + const withRegistryProvider = createHigherOrderComponent( ( WrappedComponent ) => - withRegistry( ( props ) => { - const { - useSubRegistry = true, + ( { useSubRegistry = true, ...props } ) => { + const registry = useRegistry(); + const [ subRegistries ] = useState( () => new WeakMap() ); + const subRegistry = getSubRegistry( + subRegistries, registry, - ...additionalProps - } = props; - if ( ! useSubRegistry ) { - return ; - } - - const [ subRegistry, setSubRegistry ] = useState( null ); - useEffect( () => { - const newRegistry = createRegistry( - { - 'core/block-editor': blockEditorStoreConfig, - }, - registry - ); - // Todo: The interface store should also be created per instance. - newRegistry.registerStore( 'core/editor', storeConfig ); - setSubRegistry( newRegistry ); - }, [ registry ] ); + useSubRegistry + ); - if ( ! subRegistry ) { - return null; + if ( subRegistry === registry ) { + return ; } return ( - + ); - } ), + }, 'withRegistryProvider' );