-
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.
Improve the Patterns library rendering performance (#21322)
Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>
- Loading branch information
1 parent
969bbe9
commit 1589d81
Showing
9 changed files
with
114 additions
and
21 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
40 changes: 40 additions & 0 deletions
40
packages/block-editor/src/components/block-patterns/use-async-list.js
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,40 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useReducer } from '@wordpress/element'; | ||
import { createQueue } from '@wordpress/priority-queue'; | ||
|
||
function listReducer( state, action ) { | ||
if ( action.type === 'reset' && state.length !== 0 ) { | ||
return []; | ||
} | ||
|
||
if ( action.type === 'append' ) { | ||
return [ ...state, action.item ]; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
function useAsyncList( list ) { | ||
const [ current, dispatch ] = useReducer( listReducer, [] ); | ||
|
||
useEffect( () => { | ||
dispatch( { type: 'reset' } ); | ||
const asyncQueue = createQueue(); | ||
const append = ( index = 0 ) => () => { | ||
if ( list.length <= index ) { | ||
return; | ||
} | ||
dispatch( { type: 'append', item: list[ index ] } ); | ||
asyncQueue.add( {}, append( index + 1 ) ); | ||
}; | ||
asyncQueue.add( {}, append( 0 ) ); | ||
|
||
return () => asyncQueue.reset(); | ||
}, [ list ] ); | ||
|
||
return current; | ||
} | ||
|
||
export default useAsyncList; |
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