Skip to content

Commit

Permalink
Avoid setting state for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 28, 2023
1 parent d33353b commit 62f154f
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
useMemo,
useCallback,
RawHTML,
useState,
useRef,
} from '@wordpress/element';
import {
getBlockType,
Expand Down Expand Up @@ -544,7 +544,7 @@ export default compose(
applyWithSelect,
applyWithDispatch,
( WrappedComponent ) => ( props ) => {
const [ subscriptions, setSubscriptions ] = useState( new Set() );
const subscriptions = useRef( new Set() );
const registry = useRegistry();
const attributes = useSelect(
( select ) =>
Expand All @@ -553,22 +553,21 @@ export default compose(
select( blockEditorStore ).getBlockAttributes(
props.clientId
)
).filter( ( [ key ] ) => subscriptions.has( key ) )
).filter( ( [ key ] ) => subscriptions.current.has( key ) )
),
[ subscriptions, props.clientId ]
[ props.clientId ]
);

const proxy = new Proxy( attributes, {
get( target, name ) {
if ( ! subscriptions.has( name ) ) {
window.queueMicrotask( () => {
setSubscriptions( ( subs ) => subs.add( name ) );
} );
return registry
.select( blockEditorStore )
.getBlockAttributes( props.clientId )[ name ];
if ( target.hasOwnProperty( name ) ) {
return target[ name ];
}
return target[ name ];

subscriptions.current.add( name );
return registry
.select( blockEditorStore )
.getBlockAttributes( props.clientId )[ name ];
},
} );
return <WrappedComponent { ...props } attributes={ proxy } />;
Expand Down

0 comments on commit 62f154f

Please sign in to comment.