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
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ _Properties_

### getClientIdsOfDescendants

Returns an array containing the clientIds of all descendants of the
blocks given. Ids are returned in the same order that they appear in
the editor.
Returns an array containing the clientIds of all descendants of the blocks
given. Returned ids are ordered first by the order of the ids given, then
by the order that they appear in the editor.

_Parameters_

Expand Down
40 changes: 25 additions & 15 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,27 @@ export const __unstableGetClientIdsTree = createSelector(
);

/**
* Returns an array containing the clientIds of all descendants of the
* blocks given. Ids are returned in the same order that they appear in
* the editor.
* Returns an array containing the clientIds of all descendants of the blocks
* given. Returned ids are ordered first by the order of the ids given, then
* by the order that they appear in the editor.
*
* @param {Object} state Global application state.
* @param {Array} clientIds Array of blocks to inspect.
*
* @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