-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Avoid fetching ALL reusable blocks (user patterns) on post/site editor load #58239
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' ); | ||
export const reusableBlocksSelectKey = Symbol( 'reusableBlocksSelect' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,10 @@ import { | |
import { INSERTER_PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils'; | ||
import { STORE_NAME } from './constants'; | ||
import { unlock } from '../lock-unlock'; | ||
import { selectBlockPatternsKey } from './private-keys'; | ||
import { | ||
selectBlockPatternsKey, | ||
reusableBlocksSelectKey, | ||
} from './private-keys'; | ||
|
||
export { getBlockSettings } from './get-block-settings'; | ||
|
||
|
@@ -300,26 +303,27 @@ export const getAllPatterns = createRegistrySelector( ( select ) => | |
__experimentalUserPatternCategories = [], | ||
__experimentalReusableBlocks = [], | ||
} = state.settings; | ||
const userPatterns = ( __experimentalReusableBlocks ?? [] ).map( | ||
( userPattern ) => { | ||
return { | ||
name: `core/block/${ userPattern.id }`, | ||
id: userPattern.id, | ||
type: INSERTER_PATTERN_TYPES.user, | ||
title: userPattern.title.raw, | ||
categories: userPattern.wp_pattern_category.map( | ||
( catId ) => { | ||
const category = ( | ||
__experimentalUserPatternCategories ?? [] | ||
).find( ( { id } ) => id === catId ); | ||
return category ? category.slug : catId; | ||
} | ||
), | ||
content: userPattern.content.raw, | ||
syncStatus: userPattern.wp_pattern_sync_status, | ||
}; | ||
} | ||
); | ||
const reusableBlocksSelect = state.settings[ reusableBlocksSelectKey ]; | ||
const userPatterns = ( | ||
reusableBlocksSelect | ||
? reusableBlocksSelect( select ) | ||
: __experimentalReusableBlocks ?? [] | ||
).map( ( userPattern ) => { | ||
return { | ||
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 clarity, could this function be extracted as |
||
name: `core/block/${ userPattern.id }`, | ||
id: userPattern.id, | ||
type: INSERTER_PATTERN_TYPES.user, | ||
title: userPattern.title.raw, | ||
categories: userPattern.wp_pattern_category.map( ( catId ) => { | ||
const category = ( | ||
__experimentalUserPatternCategories ?? [] | ||
).find( ( { id } ) => id === catId ); | ||
return category ? category.slug : catId; | ||
} ), | ||
content: userPattern.content.raw, | ||
syncStatus: userPattern.wp_pattern_sync_status, | ||
}; | ||
} ); | ||
return [ | ||
...userPatterns, | ||
...__experimentalBlockPatterns, | ||
|
@@ -331,6 +335,17 @@ export const getAllPatterns = createRegistrySelector( ( select ) => | |
}, getAllPatternsDependants( select ) ) | ||
); | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
export const getReusableBlocks = createRegistrySelector( | ||
( select ) => ( state ) => { | ||
const reusableBlocksSelect = state.settings[ reusableBlocksSelectKey ]; | ||
return reusableBlocksSelect | ||
? reusableBlocksSelect( select ) | ||
: state.settings.__experimentalReusableBlocks ?? EMPTY_ARRAY; | ||
} | ||
); | ||
|
||
/** | ||
* Returns the element of the last element that had focus when focus left the editor canvas. | ||
* | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
reusableBlockSelect( select )
can returnnull
if the underlying REST endpoint is still fetching. The.map
that follows will crash on it.The
?? []
bit defaults only the__experimentalReusableBlocks
value.a ? b : c ?? d
groups likea ? b : ( c ?? d )
.Also, it's unfortunate that the setting for patterns is a "fetch" function and the setting for reusable blocks is a "select" function. Is there a way to make the API consistent?
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 problem is that reusable blocks is a post type that fits with entity records, while patterns is something provided by the theme. I'm not sure what's best here. I guess we could directly fetch the reusable blocks without the entity records API. Or we move patterns back to the
core-data
store and leave it as a select function. The latter is a bit more restrictive, since you can't access the raw state to use in memoized selector dependencies (you have to call the selector which will resolve it).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.
Btw, this is why I made it a private setting, so we have room to change it later. Maybe we should make the fetch patterns function also private.