-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Unmemoize Block component selectors #58355
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ import { store as blockEditorStore } from '../../store'; | |
import { useLayout } from './layout'; | ||
import useScrollUponInsertion from './use-scroll-upon-insertion'; | ||
import { useSettings } from '../use-settings'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
|
@@ -418,11 +419,13 @@ const applyWithSelect = withSelect( ( select, { clientId, rootClientId } ) => { | |
getBlockMode, | ||
isSelectionEnabled, | ||
getTemplateLock, | ||
__unstableGetBlockWithoutInnerBlocks, | ||
getBlockWithoutAttributes, | ||
getBlockAttributes, | ||
canRemoveBlock, | ||
canMoveBlock, | ||
} = select( blockEditorStore ); | ||
const block = __unstableGetBlockWithoutInnerBlocks( clientId ); | ||
} = unlock( select( blockEditorStore ) ); | ||
const block = getBlockWithoutAttributes( clientId ); | ||
const attributes = getBlockAttributes( clientId ); | ||
const isSelected = isBlockSelected( clientId ); | ||
const templateLock = getTemplateLock( rootClientId ); | ||
const canRemove = canRemoveBlock( clientId, rootClientId ); | ||
|
@@ -432,7 +435,7 @@ const applyWithSelect = withSelect( ( select, { clientId, rootClientId } ) => { | |
// This function should never be called when a block is not present in | ||
// the state. It happens now because the order in withSelect rendering | ||
// is not correct. | ||
const { name, attributes, isValid } = block || {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For mobile, there's not really a need to keep passing attributes in the block object because mobile is not extensible by third party plugins. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this because I see that the wrapped There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, you're assigning the |
||
const { name, isValid } = block || {}; | ||
|
||
// Do not add new properties here, use `useSelect` instead to avoid | ||
// leaking new props to the public API (editor.BlockListBlock filter). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,10 @@ export function getLastInsertedBlocksClientIds( state ) { | |
return state?.lastBlockInserted?.clientIds; | ||
} | ||
|
||
export function getBlockWithoutAttributes( state, clientId ) { | ||
return state.blocks.byClientId.get( clientId ); | ||
} | ||
|
||
/** | ||
* Returns true if all of the descendants of a block with the given client ID | ||
* have an editing mode of 'disabled', or false otherwise. | ||
|
@@ -53,25 +57,17 @@ export function getLastInsertedBlocksClientIds( state ) { | |
* | ||
* @return {boolean} Whether the block descendants are disabled. | ||
*/ | ||
export const isBlockSubtreeDisabled = createSelector( | ||
( state, clientId ) => { | ||
const isChildSubtreeDisabled = ( childClientId ) => { | ||
return ( | ||
getBlockEditingMode( state, childClientId ) === 'disabled' && | ||
getBlockOrder( state, childClientId ).every( | ||
isChildSubtreeDisabled | ||
) | ||
); | ||
}; | ||
return getBlockOrder( state, clientId ).every( isChildSubtreeDisabled ); | ||
}, | ||
( state ) => [ | ||
state.blocks.parents, | ||
state.blocks.order, | ||
state.blockEditingModes, | ||
state.blockListSettings, | ||
] | ||
); | ||
export const isBlockSubtreeDisabled = ( state, clientId ) => { | ||
const isChildSubtreeDisabled = ( childClientId ) => { | ||
return ( | ||
getBlockEditingMode( state, childClientId ) === 'disabled' && | ||
getBlockOrder( state, childClientId ).every( | ||
isChildSubtreeDisabled | ||
) | ||
); | ||
}; | ||
return getBlockOrder( state, clientId ).every( isChildSubtreeDisabled ); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After #58349, this is no longer called, so the performance improvement of this PR will be lessened. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably still worth to remove the memoization I think... Also the dependencies of this selector weren't fully correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this selector should also check the editing mode of the top-level If I look at the only call in |
||
|
||
/** | ||
* Returns a tree of block objects with only clientID and innerBlocks set. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason this is better than selector cache is because it's stored by component instance, not globally.