Skip to content

Commit

Permalink
Fix asynchronousicity, re-parse after all blocks have loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Mar 28, 2023
1 parent 4bc5caf commit 9ec0608
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useEffect,
useLayoutEffect,
useMemo,
useReducer,
useState,
} from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -39,25 +39,25 @@ export const ExperimentalEditorProvider = withRegistryProvider(
children,
BlockEditorProviderComponent = ExperimentalBlockEditorProvider,
} ) => {
const [ loadedBlocks, setBlockLoaded ] = useReducer(
( alreadyLoadedBlocks, blockType ) => [
...alreadyLoadedBlocks,
blockType,
],
[]
);
const [ blocksLoaded, setBlocksLoaded ] = useState( false );
const asyncBlocks = getAsyncBlocks();

useEffect( () => {
const blockPromises = [];
asyncBlocks.forEach( ( blockGroup ) => {
blockGroup.forEach( async ( blockType ) => {
if ( loadedBlocks.includes( blockType ) ) {
return;
}
await asyncLoadBlock( blockType );
setBlockLoaded( blockType );
blockGroup.forEach( ( blockType ) => {
blockPromises.push(
new Promise( async ( resolve ) => {
await asyncLoadBlock( blockType );
resolve();
} )
);
} );
} );

Promise.allSettled( blockPromises ).then( () => {
setBlocksLoaded( true );
} );
}, [] );

const defaultBlockContext = useMemo( () => {
Expand Down Expand Up @@ -86,7 +86,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
'postType',
type,
{ id },
loadedBlocks
blocksLoaded
);
const blockEditorSettings = useBlockEditorSettings(
editorSettings,
Expand Down

0 comments on commit 9ec0608

Please sign in to comment.