Skip to content

Commit

Permalink
withRegistryProvider: prevent intermediate state with no children (Wo…
Browse files Browse the repository at this point in the history
…rdPress#61859)

Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: annezazu <[email protected]>
  • Loading branch information
5 people authored May 22, 2024
1 parent cef484e commit e7eb60a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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 (
<WrappedComponent registry={ registry } { ...props } />
);
}

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 (
<RegistryProvider value={ subRegistry }>
<WrappedComponent
registry={ subRegistry }
{ ...props }
/>
</RegistryProvider>
);
if ( subRegistry === registry ) {
return <WrappedComponent registry={ registry } { ...props } />;
}
);
},

return (
<RegistryProvider value={ subRegistry }>
<WrappedComponent registry={ subRegistry } { ...props } />
</RegistryProvider>
);
},
'withRegistryProvider'
);

Expand Down
63 changes: 32 additions & 31 deletions packages/editor/src/components/provider/with-registry-provider.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 <WrappedComponent { ...additionalProps } />;
}

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 <WrappedComponent registry={ registry } { ...props } />;
}

return (
<RegistryProvider value={ subRegistry }>
<WrappedComponent { ...additionalProps } />
<WrappedComponent registry={ subRegistry } { ...props } />
</RegistryProvider>
);
} ),
},
'withRegistryProvider'
);

Expand Down

0 comments on commit e7eb60a

Please sign in to comment.