From 1976b51d2ac6a219576680664c217f30c00608d3 Mon Sep 17 00:00:00 2001 From: myNameIsDu Date: Mon, 24 Apr 2023 19:19:01 +0800 Subject: [PATCH] pref: instead of debounce 20ms, using requestIdleCallback to delay calculation --- src/utils/useTableDimension.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/utils/useTableDimension.ts b/src/utils/useTableDimension.ts index cb1e223..afdd6ac 100644 --- a/src/utils/useTableDimension.ts +++ b/src/utils/useTableDimension.ts @@ -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 { data?: readonly Row[]; @@ -257,13 +256,21 @@ const useTableDimension = (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);