Skip to content
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

Improve Slot/Fill performance #44642

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// @ts-nocheck
/**
* External dependencies
*/
import { proxyMap } from 'valtio/utils';
/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';
import warning from '@wordpress/warning';

const SlotFillContext = createContext( {
slots: {},
fills: {},
slots: proxyMap(),
fills: proxyMap(),
registerSlot: () => {
warning(
'Components must be wrapped within `SlotFillProvider`. ' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { proxyMap } from 'valtio/utils';
/**
* WordPress dependencies
*/
import { useMemo, useCallback } from '@wordpress/element';
import { useMemo, useCallback, useRef } from '@wordpress/element';
import isShallowEqual from '@wordpress/is-shallow-equal';

/**
Expand All @@ -17,12 +17,12 @@ import isShallowEqual from '@wordpress/is-shallow-equal';
import SlotFillContext from './slot-fill-context';

function useSlotRegistry() {
const slots = proxyMap();
const fills = proxyMap();
const slots = useRef( proxyMap() );
const fills = useRef( proxyMap() );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

const registerSlot = useCallback( ( name, ref, fillProps ) => {
const slot = slots.get( name ) || {};
slots.set(
const slot = slots.current.get( name ) || {};
slots.current.set(
name,
valRef( {
...slot,
Expand All @@ -35,20 +35,20 @@ function useSlotRegistry() {
const unregisterSlot = useCallback( ( name, ref ) => {
// Make sure we're not unregistering a slot registered by another element
// See https://github.com/WordPress/gutenberg/pull/19242#issuecomment-590295412
if ( slots.get( name )?.ref === ref ) {
slots.delete( name );
if ( slots.current.get( name )?.ref === ref ) {
slots.current.delete( name );
}
}, [] );

const updateSlot = useCallback( ( name, fillProps ) => {
const slot = slots.get( name );
const slot = slots.current.get( name );
if ( ! slot ) {
return;
}

if ( ! isShallowEqual( slot.fillProps, fillProps ) ) {
slot.fillProps = fillProps;
const slotFills = fills.get( name );
const slotFills = fills.current.get( name );
if ( slotFills ) {
// Force update fills.
slotFills.map( ( fill ) => fill.current.rerender() );
Expand All @@ -57,32 +57,35 @@ function useSlotRegistry() {
}, [] );

const registerFill = useCallback( ( name, ref ) => {
fills.set( name, valRef( [ ...( fills.get( name ) || [] ), ref ] ) );
fills.current.set(
name,
valRef( [ ...( fills.current.get( name ) || [] ), ref ] )
);
}, [] );

const unregisterFill = useCallback( ( name, ref ) => {
if ( fills.get( name ) ) {
fills.set(
if ( fills.current.get( name ) ) {
fills.current.set(
name,
fills.get( name ).filter( ( fillRef ) => fillRef !== ref )
fills.current
.get( name )
.filter( ( fillRef ) => fillRef !== ref )
);
}
}, [] );

// Memoizing the return value so it can be directly passed to Provider value
const registry = useMemo(
() => ( {
slots,
fills,
slots: slots.current,
fills: fills.current,
registerSlot,
updateSlot,
unregisterSlot,
registerFill,
unregisterFill,
} ),
[
slots,
fills,
registerSlot,
updateSlot,
unregisterSlot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import SlotFillContext from './slot-fill-context';

export default function useSlotFills( name ) {
const registry = useContext( SlotFillContext );
const fills = useSnapshot( registry.fills );
const fills = useSnapshot( registry.fills, { sync: true } );
return fills.get( name );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SlotFillContext from './slot-fill-context';

export default function useSlot( name ) {
const registry = useContext( SlotFillContext );
const slots = useSnapshot( registry.slots );
const slots = useSnapshot( registry.slots, { sync: true } );
const slot = slots.get( name );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

const updateSlot = useCallback(
Expand Down