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

Editor: Memoize 'getInsertionPoint' selector #60015

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 34 additions & 16 deletions packages/editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import createSelector from 'rememo';

/**
* WordPress dependencies
*/
Expand All @@ -22,28 +27,41 @@ const EMPTY_INSERTION_POINT = {
*
* @return {Object} The root client ID, index to insert at and starting filter value.
*/
export const getInsertionPoint = createRegistrySelector(
( select ) => ( state ) => {
if ( typeof state.blockInserterPanel === 'object' ) {
return state.blockInserterPanel;
}
export const getInsertionPoint = createRegistrySelector( ( select ) =>
createSelector(
( state ) => {
if ( typeof state.blockInserterPanel === 'object' ) {
return state.blockInserterPanel;
}

if ( getRenderingMode( state ) === 'template-locked' ) {
if ( getRenderingMode( state ) === 'template-locked' ) {
const [ postContentClientId ] =
select( blockEditorStore ).getBlocksByName(
'core/post-content'
);
if ( postContentClientId ) {
return {
rootClientId: postContentClientId,
insertionIndex: undefined,
filterValue: undefined,
};
}
}

return EMPTY_INSERTION_POINT;
},
( state ) => {
const [ postContentClientId ] =
select( blockEditorStore ).getBlocksByName(
'core/post-content'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that this can lead to deoptimization: as long as there is a valid state.blockInserterPanel, the original selector never selected from blockEditorStore and didn't create a subcription to it. But the memoized version does always does the selection when calculation dependants.

There's probably no way around this other than abandoning the getInsertionPoint selector and inlining the code into the only place where it's used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, @jsnajdr! We can't remove getInsertionPoint because of BC. We can stop using it in the core, but that just avoids the issue.

);
if ( postContentClientId ) {
return {
rootClientId: postContentClientId,
insertionIndex: undefined,
filterValue: undefined,
};
}
return [
state.blockInserterPanel,
getRenderingMode( state ),
postContentClientId,
];
}

return EMPTY_INSERTION_POINT;
}
)
);

export function getListViewToggleRef( state ) {
Expand Down