Skip to content

fix: prevent memory leaks in table and sticky scroll components #1288

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

Open
wants to merge 5 commits into
base: antd-5.x
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,15 @@ function Table<RecordType extends DefaultRecordType>(

const [setScrollTarget, getScrollTarget] = useTimeoutLock(null);

// ======================= Unmount Ref =======================
const unmountedRef = React.useRef(false);
React.useEffect(() => {
unmountedRef.current = false;
return () => {
unmountedRef.current = true;
};
}, []);

function forceScroll(scrollLeft: number, target: HTMLDivElement | ((left: number) => void)) {
if (!target) {
return;
Expand All @@ -428,7 +437,9 @@ function Table<RecordType extends DefaultRecordType>(
// ref: https://github.com/ant-design/ant-design/issues/37179
if (target.scrollLeft !== scrollLeft) {
setTimeout(() => {
target.scrollLeft = scrollLeft;
if (!unmountedRef.current) {
target.scrollLeft = scrollLeft;
}
}, 0);
}
}
Expand Down Expand Up @@ -904,6 +915,7 @@ function Table<RecordType extends DefaultRecordType>(
return <TableContext.Provider value={TableContextValue}>{fullTable}</TableContext.Provider>;
}

// ========== 类型和导出补全 ==========
export type ForwardGenericTable = (<RecordType extends DefaultRecordType = any>(
props: TableProps<RecordType> & React.RefAttributes<Reference>,
) => React.ReactElement) & { displayName?: string };
Expand Down
12 changes: 11 additions & 1 deletion src/stickyScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
});
const [isActive, setActive] = React.useState(false);
const rafRef = React.useRef<number | null>(null);
// 记录上一次的 scrollParents
const lastScrollParentsRef = React.useRef<(HTMLElement | SVGElement)[]>([]);

React.useEffect(
() => () => {
Expand Down Expand Up @@ -149,14 +151,22 @@

// Loop for scroll event check
React.useEffect(() => {
if (!scrollBodyRef.current) return;
if (!scrollBodyRef.current) {
Copy link
Member

Choose a reason for hiding this comment

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

这里没括号原来是为了满足覆盖率啊 😂

return;
}

Check warning on line 156 in src/stickyScrollBar.tsx

View check run for this annotation

Codecov / codecov/patch

src/stickyScrollBar.tsx#L155-L156

Added lines #L155 - L156 were not covered by tests

// 清理上一次 scrollParents 的事件监听
lastScrollParentsRef.current.forEach(p =>
Copy link
Member

Choose a reason for hiding this comment

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

这个看起来应该是在 effect 的 cleanup 里直接做,不用额外做个 lastScrollParentsRef 来存之前的内容。直接闭包存就够了

p.removeEventListener('scroll', checkScrollBarVisible),

Check warning on line 160 in src/stickyScrollBar.tsx

View check run for this annotation

Codecov / codecov/patch

src/stickyScrollBar.tsx#L160

Added line #L160 was not covered by tests
);

const scrollParents: (HTMLElement | SVGElement)[] = [];
let parent = getDOM(scrollBodyRef.current);
while (parent) {
scrollParents.push(parent);
parent = parent.parentElement;
}
lastScrollParentsRef.current = scrollParents;

scrollParents.forEach(p => p.addEventListener('scroll', checkScrollBarVisible, false));
window.addEventListener('resize', checkScrollBarVisible, false);
Expand Down
72 changes: 71 additions & 1 deletion tests/Table.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Table, { INTERNAL_COL_DEFINE } from '../src';
import BodyRow from '../src/Body/BodyRow';
import Cell from '../src/Cell';
import { INTERNAL_HOOKS } from '../src/constant';
import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, cleanup } from '@testing-library/react';

describe('Table.Basic', () => {
const data = [
Expand Down Expand Up @@ -1372,3 +1372,73 @@ describe('Table.Basic', () => {
expect(onScroll).toHaveBeenCalled();
});
});

describe('Table memory leak and cleanup', () => {
afterEach(() => {
cleanup();
vi.clearAllTimers();
});

it('should cleanup stickyScrollBar events on unmount', () => {
const addEventListenerSpy = vi.spyOn(window, 'addEventListener');
const removeEventListenerSpy = vi.spyOn(window, 'removeEventListener');
const { unmount } = render(
<Table
columns={[{ title: 'A', dataIndex: 'a' }]}
data={[{ a: 1 }]}
scroll={{ x: 100 }}
sticky
/>,
);
unmount();
// 断言事件被移除
expect(removeEventListenerSpy).toHaveBeenCalledWith('scroll', expect.any(Function));
expect(removeEventListenerSpy).toHaveBeenCalledWith('resize', expect.any(Function));
addEventListenerSpy.mockRestore();
removeEventListenerSpy.mockRestore();
});

it('should not call setTimeout callback after unmount', () => {
vi.useFakeTimers();
const { unmount } = render(
<Table columns={[{ title: 'A', dataIndex: 'a' }]} data={[{ a: 1 }]} scroll={{ x: 100 }} />,
);
unmount();
// 触发所有定时器
vi.runAllTimers();
// 没有报错即通过
vi.useRealTimers();
});

it('should not leak when mount/unmount multiple times', () => {
for (let i = 0; i < 5; i++) {
const { unmount } = render(
<Table columns={[{ title: 'A', dataIndex: 'a' }]} data={[{ a: 1 }]} scroll={{ x: 100 }} />,
);
unmount();
}
// 没有报错即通过
});

it('should cleanup scrollParents events when scrollBodyRef changes', () => {
// 这里只能间接测试,mount/unmount 后事件应被清理
const addEventListenerSpy = vi.spyOn(window, 'addEventListener');
const removeEventListenerSpy = vi.spyOn(window, 'removeEventListener');
const { rerender, unmount } = render(
<Table
columns={[{ title: 'A', dataIndex: 'a' }]}
data={[{ a: 1 }]}
scroll={{ x: 100 }}
sticky
/>,
);
// 模拟数据变化导致 scrollBodyRef 变化
rerender(
<Table columns={[{ title: 'A', dataIndex: 'a' }]} data={[]} scroll={{ x: 100 }} sticky />,
);
unmount();
expect(removeEventListenerSpy).toHaveBeenCalledWith('scroll', expect.any(Function));
addEventListenerSpy.mockRestore();
removeEventListenerSpy.mockRestore();
});
});