-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parsing patterns when idling (performance follow-up for inserting pat…
…terns into containers) (#29444) We need to parse all the patterns to determine whether a pattern can be inserted into the selected destination. We parse them immediately when the editor loads which freezes the editor until the parser finishes. To avoid the thread blocking, this PR changes it to only parse when we have resources to do so.
- Loading branch information
1 parent
aad0052
commit 94db813
Showing
8 changed files
with
102 additions
and
25 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, select } from '@wordpress/data'; | ||
import { useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../store'; | ||
|
||
const requestIdleCallback = ( () => { | ||
if ( typeof window === 'undefined' ) { | ||
return ( callback ) => { | ||
setTimeout( () => callback( Date.now() ), 0 ); | ||
}; | ||
} | ||
|
||
return window.requestIdleCallback || window.requestAnimationFrame; | ||
} )(); | ||
|
||
const cancelIdleCallback = ( () => { | ||
if ( typeof window === 'undefined' ) { | ||
return clearTimeout; | ||
} | ||
|
||
return window.cancelIdleCallback || window.cancelAnimationFrame; | ||
} )(); | ||
|
||
export function usePreParsePatterns() { | ||
const patterns = useSelect( | ||
( _select ) => | ||
_select( blockEditorStore ).getSettings() | ||
.__experimentalBlockPatterns, | ||
[] | ||
); | ||
|
||
useEffect( () => { | ||
if ( ! patterns?.length ) { | ||
return; | ||
} | ||
|
||
let handle; | ||
let index = -1; | ||
|
||
const callback = () => { | ||
index++; | ||
if ( index >= patterns.length ) { | ||
return; | ||
} | ||
|
||
select( blockEditorStore ).__experimentalGetParsedPattern( | ||
patterns[ index ].name | ||
); | ||
|
||
handle = requestIdleCallback( callback ); | ||
}; | ||
|
||
handle = requestIdleCallback( callback ); | ||
return () => cancelIdleCallback( handle ); | ||
}, [ patterns ] ); | ||
|
||
return null; | ||
} |
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