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

SlotFill: fix a bug with storing stale fillProps #67000

Merged
merged 4 commits into from
Nov 15, 2024

Conversation

jsnajdr
Copy link
Member

@jsnajdr jsnajdr commented Nov 14, 2024

Fixes a SlotFill bug where a slot is sometimes rendered with old stale fillProps. It's described and discussed here: #65907 (comment). In short, the updateSlot method needs to check a ref value to determine if it's still the current registered slot. When an old slot is unregistered and new slot is registered, there is a short period of time where both are semi-registered and special care is needed.

That's the first commit in this PR. In addition, I'm doing a series of refactorings of SlotFill in order to put it in a better shape:

@jsnajdr jsnajdr added the [Type] Bug An existing feature does not function as intended label Nov 14, 2024
@jsnajdr jsnajdr self-assigned this Nov 14, 2024
@jsnajdr jsnajdr requested a review from ajitbohra as a code owner November 14, 2024 12:38
Copy link

github-actions bot commented Nov 14, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: Mamaduka <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added the [Package] Components /packages/components label Nov 14, 2024
Copy link

github-actions bot commented Nov 14, 2024

Flaky tests detected in 4c78204.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11854163018
📝 Reported issues:

@@ -21,8 +21,10 @@ export default function useSlot( name: SlotKey ) {

const api = useMemo(
() => ( {
updateSlot: ( fillProps: FillProps ) =>
registry.updateSlot( name, fillProps ),
updateSlot: (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this one used. or is it not used at all? I see the function arguments modified but I don't see where the calling of the function is updated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updateSlot method is used in the Slot component. Called on every render, in a effect without dependencies, to make sure that the latest fillProps is always synced promptly to the registry.

The bug being fixed is triggered when, for any reason, the Slot is rerendered one more time before unmounting. Then updateSlot would store a stale fillProps value at a time when a new Slot is already being mounted. The unregisterSlot method already has exactly the same ref check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this one updateSlot that is returned from useSlot, is not used right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, this is a good observation. The updateSlot and unregisterSlot functions returned from useSlot are not really used. And I think I could get rid of the entire api object and return only the slot. I'll do that in a followup PR.

const fillPropsRef = useRef( fillProps );
useLayoutEffect( () => {
fillPropsRef.current = fillProps;
}, [ fillProps ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general this is an indication that something should have been defined using the new useEvent maybe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately useEvent is specialized to support only callbacks. Here we have a value that is a regular object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was thinking is that we could do something like

const registerSlotEffect = useEvent(() => {
   registerSlot( name, ref, fillProps );
} );

useEffect(() => {
   registerSlotEffect();
}, [ name ] );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if we really wanted to use useEvent, it's possible, but it doesn't really improve the code IMO. Also the fillPropsRef.current value is used in two effect hooks, which makes useEvent even less attractive.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some random comments but to be honest, this one is hard to review. I think I'd just to trust the tests and your confidence in this PR :)

@jsnajdr jsnajdr force-pushed the fix/slot-fill-update-slot branch from 01c1142 to 4c78204 Compare November 15, 2024 10:00
@jsnajdr jsnajdr changed the title SlotFill: fix a bug with storing stale fillProps + several refactors SlotFill: fix a bug with storing stale fillProps Nov 15, 2024
@jsnajdr
Copy link
Member Author

jsnajdr commented Nov 15, 2024

this one is hard to review.

OK, I updated the PR to be smaller and more focused. I'll do the bigger refactors separately.

}

slots.delete( name );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'm just tidying up the code, no behavior change. unregisterSlot and updateSlot now do the same ref code and the code looks also the same.

if ( ! fillsForName ) {
return;
}
const unregisterFill: SlotFillBubblesVirtuallyContext[ 'unregisterFill' ] =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'm fixing a typo in the type: changing registerFill to unregisterFill. Both functions have the same signature, so the bug didn't have any observable consequences, but worth fixing anyway.

useLayoutEffect( () => {
registry.updateSlot( name, fillProps );
registry.updateSlot( name, ref, fillPropsRef.current );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a critical line in the patch, adding a ref param to the updateSlot call.

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

if ( slot.ref !== ref ) {
return;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is second critical line of the patch, adding a check if ref matches.

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @jsnajdr!

P.S. Inline comments were very helpful!

@jsnajdr jsnajdr enabled auto-merge (squash) November 15, 2024 11:11
@jsnajdr jsnajdr merged commit c3a9c1a into trunk Nov 15, 2024
64 checks passed
@jsnajdr jsnajdr deleted the fix/slot-fill-update-slot branch November 15, 2024 11:34
@github-actions github-actions bot added this to the Gutenberg 19.8 milestone Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants