Skip to content

Commit

Permalink
[core][data grid] Fix useTimeout clear lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Dec 29, 2024
1 parent dac7c3d commit b407d7d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
unstable_useEventCallback as useEventCallback,
} from '@mui/utils';
import { forwardRef } from '@mui/x-internals/forwardRef';
import { useOnMount } from '../../hooks/utils/useOnMount';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
import { useGridPrivateApiContext } from '../../hooks/utils/useGridPrivateApiContext';
import { gridDimensionsSelector, useGridSelector } from '../../hooks';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
Expand Down Expand Up @@ -95,10 +95,7 @@ const GridVirtualScrollbar = forwardRef<HTMLDivElement, GridVirtualScrollbarProp

const onScrollerScroll = useEventCallback(() => {
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current;
if (!scrollbar) {
return;
}
const scrollbar = scrollbarRef.current!;

if (scroller[propertyScroll] === lastPosition.current) {
return;
Expand All @@ -118,10 +115,7 @@ const GridVirtualScrollbar = forwardRef<HTMLDivElement, GridVirtualScrollbarProp

const onScrollbarScroll = useEventCallback(() => {
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current;
if (!scrollbar) {
return;
}
const scrollbar = scrollbarRef.current!;

if (isLocked.current) {
isLocked.current = false;
Expand All @@ -133,7 +127,7 @@ const GridVirtualScrollbar = forwardRef<HTMLDivElement, GridVirtualScrollbarProp
scroller[propertyScroll] = value * contentSize;
});

useOnMount(() => {
useEnhancedEffect(() => {
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current!;
scroller.addEventListener('scroll', onScrollerScroll, { capture: true });
Expand All @@ -142,7 +136,8 @@ const GridVirtualScrollbar = forwardRef<HTMLDivElement, GridVirtualScrollbarProp
scroller.removeEventListener('scroll', onScrollerScroll, { capture: true });
scrollbar.removeEventListener('scroll', onScrollbarScroll, { capture: true });
};
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

React.useEffect(() => {
const content = contentRef.current!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
unstable_useEventCallback as useEventCallback,
} from '@mui/utils';
import useLazyRef from '@mui/utils/useLazyRef';
import useTimeout from '@mui/utils/useTimeout';
import { useRtl } from '@mui/system/RtlProvider';
import reactMajor from '@mui/x-internals/reactMajor';
import type { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
import { useGridPrivateApiContext } from '../../utils/useGridPrivateApiContext';
import { useGridRootProps } from '../../utils/useGridRootProps';
import { useTimeout } from '../../utils/useTimeout';
import { useGridSelector } from '../../utils/useGridSelector';
import { useRunOnce } from '../../utils/useRunOnce';
import {
Expand Down Expand Up @@ -262,14 +262,9 @@ export const useGridVirtualScroller = () => {
);

const triggerUpdateRenderContext = useEventCallback(() => {
const scroller = scrollerRef.current;
if (!scroller) {
return undefined;
}

const newScroll = {
top: scroller.scrollTop,
left: scroller.scrollLeft,
top: scrollerRef.current!.scrollTop,
left: scrollerRef.current!.scrollLeft,
};

const dx = newScroll.left - scrollPosition.current.left;
Expand Down
48 changes: 47 additions & 1 deletion packages/x-data-grid/src/hooks/utils/useTimeout.ts
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
export { default as useTimeout } from '@mui/utils/useTimeout';
'use client';
/**
* TODO, remove this file, have dependents import from:
* import useTimeout from '@mui/utils/useTimeout';
* directly.
*/
import useLazyRef from '@mui/utils/useLazyRef';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';

class Timeout {
static create() {
return new Timeout();
}

currentId: ReturnType<typeof setTimeout> | null = null;

/**
* Executes `fn` after `delay`, clearing any previously scheduled call.
*/
start(delay: number, fn: Function) {
this.clear();
this.currentId = setTimeout(() => {
this.currentId = null;
fn();
}, delay);
}

clear = () => {
if (this.currentId !== null) {
clearTimeout(this.currentId);
this.currentId = null;
}
};

disposeEffect = () => {
return this.clear;
};
}

export function useTimeout() {
const timeout = useLazyRef(Timeout.create).current;

// eslint-disable-next-line react-hooks/exhaustive-deps
useEnhancedEffect(timeout.disposeEffect, []);

return timeout;
}
1 change: 0 additions & 1 deletion packages/x-tree-view/src/internals/hooks/useTimeout.ts

This file was deleted.

0 comments on commit b407d7d

Please sign in to comment.