2021-11-02 08:07:35 +00:00
|
|
|
import getRequestAnimationFrame, { cancelRequestAnimationFrame } from './getRequestAnimationFrame';
|
2020-10-13 14:26:56 +00:00
|
|
|
|
2021-11-02 08:07:35 +00:00
|
|
|
const oriRaf = getRequestAnimationFrame();
|
2018-12-13 13:26:21 +00:00
|
|
|
|
2021-11-02 08:07:35 +00:00
|
|
|
export type RafFrame = {
|
|
|
|
id: number;
|
|
|
|
};
|
2018-12-13 13:26:21 +00:00
|
|
|
// Support call raf with delay specified frame
|
2021-11-02 08:07:35 +00:00
|
|
|
export default function raf(callback: () => void, delayFrames = 1): { id: number } {
|
2020-10-13 14:26:56 +00:00
|
|
|
let restFrames: number = delayFrames;
|
2018-12-13 13:26:21 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function internalCallback() {
|
|
|
|
restFrames -= 1;
|
2018-12-13 13:26:21 +00:00
|
|
|
|
|
|
|
if (restFrames <= 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback();
|
2018-12-13 13:26:21 +00:00
|
|
|
} else {
|
2021-11-02 08:07:35 +00:00
|
|
|
frame.id = oriRaf(internalCallback);
|
2018-12-13 13:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 08:07:35 +00:00
|
|
|
const frame = {
|
|
|
|
id: oriRaf(internalCallback),
|
|
|
|
};
|
2018-12-13 13:26:21 +00:00
|
|
|
|
2021-11-02 08:07:35 +00:00
|
|
|
return frame;
|
2018-12-13 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 08:07:35 +00:00
|
|
|
raf.cancel = function cancel(frame?: { id: number }) {
|
|
|
|
if (!frame) return;
|
2020-10-13 14:26:56 +00:00
|
|
|
|
2021-11-02 08:07:35 +00:00
|
|
|
cancelRequestAnimationFrame(frame.id);
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|