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

fix: don't call clearTimeout unnecessarily #30109

Closed
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: don't call clearTimeout unnecessarily",
"packageName": "@fluentui/react-popover",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,22 @@ export const usePopover_unstable = (props: PopoverProps): PopoverState => {

const [open, setOpenState] = useOpenState(initialState);

const setOpenTimeoutRef = React.useRef(0);
const setOpenTimeoutRef = React.useRef(-1);
const clearOpenTimeout = React.useCallback(() => {
if (setOpenTimeoutRef.current >= 0) {
clearTimeout(setOpenTimeoutRef.current);
setOpenTimeoutRef.current = -1;
}
}, []);

const setOpenTimeout = (e: OpenPopoverEvents, shouldOpen: boolean) => {
setOpenTimeoutRef.current = setTimeout(() => {
setOpenState(e, shouldOpen);
}, props.mouseLeaveDelay ?? 500);
};

const setOpen = useEventCallback((e: OpenPopoverEvents, shouldOpen: boolean) => {
clearTimeout(setOpenTimeoutRef.current);
clearOpenTimeout();
if (!(e instanceof Event) && e.persist) {
// < React 17 still uses pooled synthetic events
e.persist();
Expand All @@ -73,9 +85,7 @@ export const usePopover_unstable = (props: PopoverProps): PopoverState => {
// FIXME leaking Node timeout type
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
setOpenTimeoutRef.current = setTimeout(() => {
setOpenState(e, shouldOpen);
}, props.mouseLeaveDelay ?? 500);
setOpenTimeout(e, shouldOpen);
} else {
setOpenState(e, shouldOpen);
}
Expand All @@ -85,9 +95,9 @@ export const usePopover_unstable = (props: PopoverProps): PopoverState => {
// Setting state after a component unmounts can cause memory leaks
React.useEffect(() => {
return () => {
clearTimeout(setOpenTimeoutRef.current);
clearOpenTimeout();
};
}, []);
}, [clearOpenTimeout]);

const toggleOpen = React.useCallback<PopoverState['toggleOpen']>(
e => {
Expand Down
Loading