Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Optimize __unstableGetVisibleBlocks() #47681

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ const MILLISECONDS_PER_WEEK = 7 * 24 * 3600 * 1000;
*/
const EMPTY_ARRAY = [];

/**
* Shared reference to an empty Set for cases where it is important to avoid
* returning a new Set reference on every invocation, as in a connected or
* other pure component which performs `shouldComponentUpdate` check on props.
* This should be used as a last resort, since the normalized data should be
* maintained by the reducer result in state.
*
* @type {Set}
*/
const EMPTY_SET = new Set();

/**
* Returns a block's name given its client ID, or null if no block exists with
* the client ID.
Expand Down Expand Up @@ -2722,11 +2733,15 @@ export function isBlockVisible( state, clientId ) {
*/
export const __unstableGetVisibleBlocks = createSelector(
( state ) => {
return new Set(
const visibleBlocks = new Set(
Object.keys( state.blockVisibility ).filter(
( key ) => state.blockVisibility[ key ]
)
);
if ( visibleBlocks.size === 0 ) {
return EMPTY_SET;
}
return visibleBlocks;
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
},
( state ) => [ state.blockVisibility ]
);
Expand Down