PromoCursed/node_modules/rc-motion/es/hooks/useNextFrame.js

30 lines
819 B
JavaScript
Raw Normal View History

2024-08-20 23:25:37 +04:00
import raf from "rc-util/es/raf";
import * as React from 'react';
export default (function () {
var nextFrameRef = React.useRef(null);
function cancelNextFrame() {
raf.cancel(nextFrameRef.current);
}
function nextFrame(callback) {
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
cancelNextFrame();
var nextFrameId = raf(function () {
if (delay <= 1) {
callback({
isCanceled: function isCanceled() {
return nextFrameId !== nextFrameRef.current;
}
});
} else {
nextFrame(callback, delay - 1);
}
});
nextFrameRef.current = nextFrameId;
}
React.useEffect(function () {
return function () {
cancelNextFrame();
};
}, []);
return [nextFrame, cancelNextFrame];
});