Skip to content

Commit 257f7f0

Browse files
committed
refactor: raf
1 parent b77ca0e commit 257f7f0

File tree

16 files changed

+63
-63
lines changed

16 files changed

+63
-63
lines changed

components/_util/PortalWrapper.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {
1414
} from 'vue';
1515
import canUseDom from './canUseDom';
1616
import ScrollLocker from '../vc-util/Dom/scrollLocker';
17-
import type { RafFrame } from './raf';
18-
import wrapperRaf from './raf';
17+
import raf from './raf';
1918

2019
let openCount = 0;
2120
const supportDom = canUseDom();
@@ -62,7 +61,7 @@ export default defineComponent({
6261
setup(props, { slots }) {
6362
const container = ref<HTMLElement>();
6463
const componentRef = ref();
65-
const rafId = ref<RafFrame>();
64+
const rafId = ref<number>();
6665
const scrollLocker = new ScrollLocker({
6766
container: getParent(props.getContainer) as HTMLElement,
6867
});
@@ -176,7 +175,7 @@ export default defineComponent({
176175

177176
nextTick(() => {
178177
if (!attachToParent()) {
179-
rafId.value = wrapperRaf(() => {
178+
rafId.value = raf(() => {
180179
instance.update();
181180
});
182181
}
@@ -190,7 +189,7 @@ export default defineComponent({
190189
openCount = visible && openCount ? openCount - 1 : openCount;
191190
}
192191
removeCurrentContainer();
193-
wrapperRaf.cancel(rafId.value);
192+
raf.cancel(rafId.value);
194193
});
195194

196195
return () => {

components/_util/hooks/useLayoutState.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Ref } from 'vue';
22
import { onBeforeUnmount, ref } from 'vue';
3-
import wrapperRaf from '../raf';
3+
import raf from '../raf';
44

55
export type Updater<State> = (prev: State) => State;
66
/**
@@ -15,10 +15,10 @@ export function useLayoutState<State>(
1515
let updateBatchRef = [];
1616
const rafRef = ref();
1717
function setFrameState(updater: Updater<State>) {
18-
wrapperRaf.cancel(rafRef.value);
18+
raf.cancel(rafRef.value);
1919
updateBatchRef.push(updater);
2020

21-
rafRef.value = wrapperRaf(() => {
21+
rafRef.value = raf(() => {
2222
const prevBatch = updateBatchRef;
2323
// const prevState = stateRef.value;
2424
updateBatchRef = [];
@@ -34,7 +34,7 @@ export function useLayoutState<State>(
3434
}
3535

3636
onBeforeUnmount(() => {
37-
wrapperRaf.cancel(rafRef.value);
37+
raf.cancel(rafRef.value);
3838
});
3939

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

components/_util/raf.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
1-
import getRequestAnimationFrame, { cancelRequestAnimationFrame } from './getRequestAnimationFrame';
1+
let raf = (callback: FrameRequestCallback) => +setTimeout(callback, 16);
2+
let caf = (num: number) => clearTimeout(num);
23

3-
const oriRaf = getRequestAnimationFrame();
4+
if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
5+
raf = (callback: FrameRequestCallback) => window.requestAnimationFrame(callback);
6+
caf = (handle: number) => window.cancelAnimationFrame(handle);
7+
}
48

5-
export type RafFrame = {
6-
id: number;
7-
};
8-
// Support call raf with delay specified frame
9-
export default function raf(callback: () => void, delayFrames = 1): { id: number } {
10-
let restFrames: number = delayFrames;
9+
let rafUUID = 0;
10+
const rafIds = new Map<number, number>();
11+
12+
function cleanup(id: number) {
13+
rafIds.delete(id);
14+
}
1115

12-
function internalCallback() {
13-
restFrames -= 1;
16+
export default function wrapperRaf(callback: () => void, times = 1): number {
17+
rafUUID += 1;
18+
const id = rafUUID;
1419

15-
if (restFrames <= 0) {
20+
function callRef(leftTimes: number) {
21+
if (leftTimes === 0) {
22+
// Clean up
23+
cleanup(id);
24+
25+
// Trigger
1626
callback();
1727
} else {
18-
frame.id = oriRaf(internalCallback);
28+
// Next raf
29+
const realId = raf(() => {
30+
callRef(leftTimes - 1);
31+
});
32+
33+
// Bind real raf id
34+
rafIds.set(id, realId);
1935
}
2036
}
2137

22-
const frame = {
23-
id: oriRaf(internalCallback),
24-
};
38+
callRef(times);
2539

26-
return frame;
40+
return id;
2741
}
2842

29-
raf.cancel = function cancel(frame?: { id: number }) {
30-
if (!frame) return;
31-
32-
cancelRequestAnimationFrame(frame.id);
43+
wrapperRaf.cancel = (id: number) => {
44+
const realId = rafIds.get(id);
45+
cleanup(realId);
46+
return caf(realId);
3347
};

components/_util/throttleByAnimationFrame.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type { RafFrame } from './raf';
21
import raf from './raf';
32

43
export default function throttleByAnimationFrame(fn: (...args: any[]) => void) {
5-
let requestId: RafFrame;
4+
let requestId: number;
65

76
const later = (args: any[]) => () => {
87
requestId = null;

components/menu/src/PopupTrigger.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { computed, defineComponent, onBeforeUnmount, ref, watch } from 'vue';
44
import type { MenuMode } from './interface';
55
import { useInjectForceRender, useInjectMenu } from './hooks/useMenuContext';
66
import { placements, placementsRtl } from './placements';
7-
import type { RafFrame } from '../../_util/raf';
87
import raf from '../../_util/raf';
98
import classNames from '../../_util/classNames';
109
import { getTransitionProps } from '../../_util/transition';
@@ -54,7 +53,7 @@ export default defineComponent({
5453

5554
const popupPlacement = computed(() => popupPlacementMap[props.mode]);
5655

57-
const visibleRef = ref<RafFrame>();
56+
const visibleRef = ref<number>();
5857
watch(
5958
() => props.visible,
6059
visible => {

components/slider/SliderTooltip.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { onBeforeUnmount, watch, onActivated, defineComponent, ref } from 'vue';
22
import Tooltip, { tooltipProps } from '../tooltip';
3-
import type { RafFrame } from '../_util/raf';
43
import raf from '../_util/raf';
54

65
export default defineComponent({
@@ -10,7 +9,7 @@ export default defineComponent({
109
setup(props, { attrs, slots }) {
1110
const innerRef = ref<any>(null);
1211

13-
const rafRef = ref<RafFrame>(null);
12+
const rafRef = ref<number>(null);
1413

1514
function cancelKeepAlign() {
1615
raf.cancel(rafRef.value!);

components/tabs/src/TabNavList/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import { onBeforeUnmount, defineComponent, ref, watch, watchEffect, computed } f
2222
import PropTypes from '../../../_util/vue-types';
2323
import useSyncState from '../hooks/useSyncState';
2424
import useState from '../../../_util/hooks/useState';
25-
import type { RafFrame } from '../../../_util/raf';
26-
import wrapperRaf from '../../../_util/raf';
25+
import raf from '../../../_util/raf';
2726
import classNames from '../../../_util/classNames';
2827
import ResizeObserver from '../../../vc-resize-observer';
2928
import { toPx } from '../../../_util/util';
@@ -340,9 +339,9 @@ export default defineComponent({
340339
const activeTabOffset = computed(() => tabOffsets.value.get(props.activeKey));
341340

342341
// Delay set ink style to avoid remove tab blink
343-
const inkBarRafRef = ref<RafFrame>();
342+
const inkBarRafRef = ref<number>();
344343
const cleanInkBarRaf = () => {
345-
wrapperRaf.cancel(inkBarRafRef.value);
344+
raf.cancel(inkBarRafRef.value);
346345
};
347346

348347
watch([activeTabOffset, tabPositionTopOrBottom, () => props.rtl], () => {
@@ -364,7 +363,7 @@ export default defineComponent({
364363
}
365364

366365
cleanInkBarRaf();
367-
inkBarRafRef.value = wrapperRaf(() => {
366+
inkBarRafRef.value = raf(() => {
368367
setInkStyle(newInkStyle);
369368
});
370369
});

components/tabs/src/hooks/useRaf.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import type { Ref } from 'vue';
22
import { ref, onBeforeUnmount } from 'vue';
3-
import type { RafFrame } from '../../../_util/raf';
4-
import wrapperRaf from '../../../_util/raf';
3+
import raf from '../../../_util/raf';
54

65
export default function useRaf<Callback extends Function>(callback: Callback) {
7-
const rafRef = ref<RafFrame>();
6+
const rafRef = ref<number>();
87
const removedRef = ref(false);
98

109
function trigger(...args: any[]) {
1110
if (!removedRef.value) {
12-
wrapperRaf.cancel(rafRef.value);
13-
rafRef.value = wrapperRaf(() => {
11+
raf.cancel(rafRef.value);
12+
rafRef.value = raf(() => {
1413
callback(...args);
1514
});
1615
}
1716
}
1817

1918
onBeforeUnmount(() => {
2019
removedRef.value = true;
21-
wrapperRaf.cancel(rafRef.value);
20+
raf.cancel(rafRef.value);
2221
});
2322

2423
return trigger;

components/vc-image/src/hooks/useFrameSetState.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import type { RafFrame } from '../../../_util/raf';
21
import raf from '../../../_util/raf';
32
import { onMounted, reactive, ref } from 'vue';
43

54
type SetActionType<T> = Partial<T> | ((state: T) => Partial<T>);
65
export default function useFrameSetState<T extends object>(
76
initial: T,
87
): [Record<string, any>, (newState: SetActionType<T>) => void] {
9-
const frame = ref<RafFrame>(null);
8+
const frame = ref<number>(null);
109
const state = reactive({ ...initial });
1110
const queue = ref<SetActionType<T>[]>([]);
1211

components/vc-picker/hooks/useHoverValue.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { RafFrame } from '../../_util/raf';
21
import raf from '../../_util/raf';
32
import type { ComputedRef, Ref, UnwrapRef } from 'vue';
43
import { ref, onBeforeUnmount, watch } from 'vue';
@@ -10,7 +9,7 @@ export default function useHoverValue<DateType>(
109
{ formatList, generateConfig, locale }: ValueTextConfig<DateType>,
1110
): [ComputedRef<string>, (date: DateType) => void, (immediately?: boolean) => void] {
1211
const innerValue = ref<DateType>(null);
13-
let rafId: RafFrame;
12+
let rafId: number;
1413

1514
function setValue(val: DateType, immediately = false) {
1615
raf.cancel(rafId);

0 commit comments

Comments
 (0)