-
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.
Block preview: build in async rendering (#60425)
Co-authored-by: ellatrix <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: oandregal <[email protected]>
- Loading branch information
1 parent
0e025fd
commit b59d363
Showing
10 changed files
with
70 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useState, flushSync } from '@wordpress/element'; | ||
import { createQueue } from '@wordpress/priority-queue'; | ||
|
||
const blockPreviewQueue = createQueue(); | ||
|
||
/** | ||
* Renders a component at the next idle time. | ||
* @param {*} props | ||
*/ | ||
export function Async( { children, placeholder } ) { | ||
const [ shouldRender, setShouldRender ] = useState( false ); | ||
|
||
// In the future, we could try to use startTransition here, but currently | ||
// react will batch all transitions, which means all previews will be | ||
// rendered at the same time. | ||
// https://react.dev/reference/react/startTransition#caveats | ||
// > If there are multiple ongoing Transitions, React currently batches them | ||
// > together. This is a limitation that will likely be removed in a future | ||
// > release. | ||
|
||
useEffect( () => { | ||
const context = {}; | ||
blockPreviewQueue.add( context, () => { | ||
// Synchronously run all renders so it consumes timeRemaining. | ||
// See https://github.com/WordPress/gutenberg/pull/48238 | ||
flushSync( () => { | ||
setShouldRender( true ); | ||
} ); | ||
} ); | ||
return () => { | ||
blockPreviewQueue.cancel( context ); | ||
}; | ||
}, [] ); | ||
|
||
if ( ! shouldRender ) { | ||
return placeholder; | ||
} | ||
|
||
return children; | ||
} |
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