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

Optimize getClientIdsOfDescendants and getClientIdsWithDescendants selectors. #40054

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Optimize getClientIdsOfDescendants and getClientIdsWithDescendants.
  • Loading branch information
ZebulanStanphill committed Apr 5, 2022
commit 0fddb2530ceda7a7611c563163f879ddc8f569c1
34 changes: 22 additions & 12 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,18 @@ export const __unstableGetClientIdsTree = createSelector(
*
* @return {Array} ids of descendants.
*/
export const getClientIdsOfDescendants = ( state, clientIds ) =>
clientIds.flatMap( ( clientId ) =>
getBlockOrder( state, clientId ).flatMap( ( descendantId ) => [
descendantId,
...getClientIdsOfDescendants( state, [ descendantId ] ),
] )
);
export const getClientIdsOfDescendants = ( state, clientIds ) => {
const collectedIds = [];
for ( const givenId of clientIds ) {
for ( const descendantId of getBlockOrder( state, givenId ) ) {
collectedIds.push(
descendantId,
...getClientIdsOfDescendants( state, [ descendantId ] )
);
}
}
return collectedIds;
};

/**
* Returns an array containing the clientIds of the top-level blocks and
Expand All @@ -243,11 +248,16 @@ export const getClientIdsOfDescendants = ( state, clientIds ) =>
* @return {Array} ids of top-level and descendant blocks.
*/
export const getClientIdsWithDescendants = createSelector(
( state ) =>
getBlockOrder( state ).flatMap( ( topLevelId ) => [
topLevelId,
...getClientIdsOfDescendants( state, [ topLevelId ] ),
] ),
( state ) => {
const collectedIds = [];
for ( const topLevelId of getBlockOrder( state ) ) {
collectedIds.push(
topLevelId,
...getClientIdsOfDescendants( state, [ topLevelId ] )
);
}
return collectedIds;
},
( state ) => [ state.blocks.order ]
);

Expand Down