Skip to content

Commit 9322965

Browse files
committed
perf: table, close #4787
1 parent 816665f commit 9322965

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

components/_util/raf.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let id = 0;
66
const ids: RafMap = {};
77

88
// Support call raf with delay specified frame
9-
export default function wrapperRaf(callback: () => void, delayFrames = 1): number {
9+
export default function raf(callback: () => void, delayFrames = 1): number {
1010
const myId: number = id++;
1111
let restFrames: number = delayFrames;
1212

@@ -26,11 +26,11 @@ export default function wrapperRaf(callback: () => void, delayFrames = 1): numbe
2626
return myId;
2727
}
2828

29-
wrapperRaf.cancel = function cancel(pid?: number) {
29+
raf.cancel = function cancel(pid?: number) {
3030
if (pid === undefined) return;
3131

3232
cancelAnimationFrame(ids[pid]);
3333
delete ids[pid];
3434
};
3535

36-
wrapperRaf.ids = ids; // export this for test usage
36+
raf.ids = ids; // export this for test usage

components/vc-table/Table.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,15 @@ export default defineComponent<TableProps<DefaultRecordType>>({
448448
onScroll({ currentTarget: scrollBodyRef.value });
449449
}
450450
};
451-
451+
let timtout;
452452
const onFullTableResize = ({ width }) => {
453-
if (width !== componentWidth.value) {
454-
triggerOnScroll();
455-
componentWidth.value = fullTableRef.value ? fullTableRef.value.offsetWidth : width;
456-
}
453+
clearTimeout(timtout);
454+
timtout = setTimeout(() => {
455+
if (width !== componentWidth.value) {
456+
triggerOnScroll();
457+
componentWidth.value = fullTableRef.value ? fullTableRef.value.offsetWidth : width;
458+
}
459+
}, 100);
457460
};
458461

459462
watch([horizonScroll, () => props.data, () => props.columns], () => {

components/vc-table/hooks/useFrame.ts

+14-26
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
1+
import raf from '../../_util/raf';
12
import type { Ref, UnwrapRef } from 'vue';
2-
import { onBeforeUnmount, ref } from 'vue';
3+
import { onBeforeUnmount, ref, shallowRef } from 'vue';
34

45
export type Updater<State> = (prev: State) => State;
56

6-
/**
7-
* Execute code before next frame but async
8-
*/
97
export function useLayoutState<State>(
108
defaultState: State,
119
): [Ref<State>, (updater: Updater<State>) => void] {
12-
const stateRef = ref<State>(defaultState);
13-
// const [, forceUpdate] = useState({});
14-
15-
const lastPromiseRef = ref<Promise<void>>(null);
16-
const updateBatchRef = ref<Updater<State>[]>([]);
10+
const stateRef = shallowRef<State>(defaultState);
11+
let rafId: number;
12+
const updateBatchRef = shallowRef<Updater<State>[]>([]);
1713
function setFrameState(updater: Updater<State>) {
1814
updateBatchRef.value.push(updater);
19-
20-
const promise = Promise.resolve();
21-
lastPromiseRef.value = promise;
22-
23-
promise.then(() => {
24-
if (lastPromiseRef.value === promise) {
25-
const prevBatch = updateBatchRef.value;
26-
// const prevState = stateRef.value;
27-
updateBatchRef.value = [];
28-
29-
prevBatch.forEach(batchUpdater => {
30-
stateRef.value = batchUpdater(stateRef.value as State) as UnwrapRef<State>;
31-
});
32-
33-
lastPromiseRef.value = null;
34-
}
15+
raf.cancel(rafId);
16+
rafId = raf(() => {
17+
const prevBatch = updateBatchRef.value;
18+
// const prevState = stateRef.value;
19+
updateBatchRef.value = [];
20+
prevBatch.forEach(batchUpdater => {
21+
stateRef.value = batchUpdater(stateRef.value as State);
22+
});
3523
});
3624
}
3725
onBeforeUnmount(() => {
38-
lastPromiseRef.value = null;
26+
raf.cancel(rafId);
3927
});
4028

4129
return [stateRef as Ref<State>, setFrameState];

0 commit comments

Comments
 (0)