mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-15 17:31:25 +08:00
52cb37f487
* chore: remove throttleByAnimationFrameDecorator * fix * fix
30 lines
623 B
TypeScript
30 lines
623 B
TypeScript
import raf from 'rc-util/lib/raf';
|
|
|
|
type throttledFn = (...args: any[]) => void;
|
|
|
|
type throttledCancelFn = { cancel: () => void };
|
|
|
|
function throttleByAnimationFrame<T extends any[]>(fn: (...args: T) => void) {
|
|
let requestId: number | null;
|
|
|
|
const later = (args: T) => () => {
|
|
requestId = null;
|
|
fn(...args);
|
|
};
|
|
|
|
const throttled: throttledFn & throttledCancelFn = (...args: T) => {
|
|
if (requestId == null) {
|
|
requestId = raf(later(args));
|
|
}
|
|
};
|
|
|
|
throttled.cancel = () => {
|
|
raf.cancel(requestId!);
|
|
requestId = null;
|
|
};
|
|
|
|
return throttled;
|
|
}
|
|
|
|
export default throttleByAnimationFrame;
|