fix: set requestId to null after fn was cancelled (#34858)

This commit is contained in:
Tmk 2022-04-05 13:01:18 +08:00 committed by GitHub
parent 27bb1ea4ae
commit c4ffbff841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,20 +1,26 @@
import raf from 'rc-util/lib/raf'; import raf from 'rc-util/lib/raf';
export function throttleByAnimationFrame(fn: (...args: any[]) => void) { export function throttleByAnimationFrame<T extends unknown[]>(fn: (...args: T) => void) {
let requestId: number | null; let requestId: number | null;
const later = (args: any[]) => () => { const later = (args: T) => () => {
requestId = null; requestId = null;
fn(...args); fn(...args);
}; };
const throttled = (...args: any[]) => { const throttled: {
(...args: T): void;
cancel: () => void;
} = (...args: T) => {
if (requestId == null) { if (requestId == null) {
requestId = raf(later(args)); requestId = raf(later(args));
} }
}; };
(throttled as any).cancel = () => raf.cancel(requestId!); throttled.cancel = () => {
raf.cancel(requestId!);
requestId = null;
};
return throttled; return throttled;
} }