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

pref: instead of debounce 20ms, using requestIdleCallback to delay calculation #425

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 15 additions & 9 deletions src/utils/useTableDimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import useMount from './useMount';
import useUpdateLayoutEffect from './useUpdateLayoutEffect';
import isNumberOrTrue from './isNumberOrTrue';
import { RowDataType, ElementOffset } from '../@types/common';
import debounce from 'lodash/debounce';

interface TableDimensionProps<Row, Key> {
data?: readonly Row[];
Expand Down Expand Up @@ -59,7 +58,6 @@ const useTableDimension = <Row extends RowDataType, Key>(props: TableDimensionPr
children,
expandedRowKeys,
showHeader,
bordered,
onTableResizeChange,
onTableScroll
} = props;
Expand Down Expand Up @@ -257,13 +255,21 @@ const useTableDimension = <Row extends RowDataType, Key>(props: TableDimensionPr
calculateTableHeight(entries[0].contentRect.height);
});
containerResizeObserver.current.observe(tableRef?.current?.parentNode as Element);
const changeTableWidthWhenResize = debounce(entries => {
const { width } = entries[0].contentRect;
// bordered table width is 1px larger than the container width. fix: #405 #404
const widthWithBorder = width + 2;

calculateTableWidth(bordered ? widthWithBorder : width);
}, 20);
let idleCallbackId: null | number = null;
const changeTableWidthWhenResize = function (entries) {
if (idleCallbackId) {
window.cancelIdleCallback(idleCallbackId);
}
idleCallbackId = window.requestIdleCallback(deadline => {
Copy link
Member

Choose a reason for hiding this comment

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

Safari browser does not support requestIdleCallback.

// if idle time >= 10ms, then we can judge other tasks have completed
if (deadline.timeRemaining() >= 10) {
idleCallbackId = null;
calculateTableWidth(entries[0].contentRect.width);
} else {
changeTableWidthWhenResize(entries);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
changeTableWidthWhenResize(entries);
// If there is not enough idle time, try again in a future idle period.
// Use setTimeout to avoid stack overflow if this function is called too frequently.
setTimeout(() => {
changeTableWidthWhenResize(entries);
}, 50);

}
});
};
resizeObserver.current = new ResizeObserver(changeTableWidthWhenResize);
resizeObserver.current.observe(tableRef?.current as Element);

Expand Down