Skip to content

Commit

Permalink
useNestedSettingsUpdate: revert deoptimization of the updateBlockList…
Browse files Browse the repository at this point in the history
…Settings actions" (#46350)
  • Loading branch information
jsnajdr authored Dec 7, 2022
1 parent 21adace commit 1d4be43
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useLayoutEffect, useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import isShallowEqual from '@wordpress/is-shallow-equal';

/**
Expand All @@ -13,6 +13,8 @@ import { getLayoutType } from '../../layouts';

/** @typedef {import('../../selectors').WPDirectInsertBlock } WPDirectInsertBlock */

const pendingSettingsUpdates = new WeakMap();

/**
* This hook is a side effect which updates the block-editor store when changes
* happen to inner block settings. The given props are transformed into a
Expand Down Expand Up @@ -46,6 +48,8 @@ export default function useNestedSettingsUpdate(
layout
) {
const { updateBlockListSettings } = useDispatch( blockEditorStore );
const registry = useRegistry();

const { blockListSettings, parentLock } = useSelect(
( select ) => {
const rootClientId =
Expand Down Expand Up @@ -97,7 +101,30 @@ export default function useNestedSettingsUpdate(
}

if ( ! isShallowEqual( blockListSettings, newSettings ) ) {
updateBlockListSettings( clientId, newSettings );
// Batch updates to block list settings to avoid triggering cascading renders
// for each container block included in a tree and optimize initial render.
// To avoid triggering updateBlockListSettings for each container block
// causing X re-renderings for X container blocks,
// we batch all the updatedBlockListSettings in a single "data" batch
// which results in a single re-render.
if ( ! pendingSettingsUpdates.get( registry ) ) {
pendingSettingsUpdates.set( registry, [] );
}
pendingSettingsUpdates
.get( registry )
.push( [ clientId, newSettings ] );
window.queueMicrotask( () => {
if ( pendingSettingsUpdates.get( registry )?.length ) {
registry.batch( () => {
pendingSettingsUpdates
.get( registry )
.forEach( ( args ) => {
updateBlockListSettings( ...args );
} );
pendingSettingsUpdates.set( registry, [] );
} );
}
} );
}
}, [
clientId,
Expand All @@ -111,5 +138,6 @@ export default function useNestedSettingsUpdate(
orientation,
updateBlockListSettings,
layout,
registry,
] );
}

0 comments on commit 1d4be43

Please sign in to comment.