Skip to content

Commit

Permalink
Pref: instead of debounce 20ms, using requestIdleCallback to delay ca…
Browse files Browse the repository at this point in the history
…lculation
  • Loading branch information
myNameIsDu committed Apr 25, 2023
1 parent a2e1b58 commit 159d2a5
Showing 1 changed file with 15 additions and 9 deletions.
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 => {
// 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);
}
});
};
resizeObserver.current = new ResizeObserver(changeTableWidthWhenResize);
resizeObserver.current.observe(tableRef?.current as Element);

Expand Down

0 comments on commit 159d2a5

Please sign in to comment.