25 lines
662 B
JavaScript
25 lines
662 B
JavaScript
import raf from "@rc-component/util/es/raf";
|
|
import * as React from 'react';
|
|
export default (() => {
|
|
const nextFrameRef = React.useRef(null);
|
|
function cancelNextFrame() {
|
|
raf.cancel(nextFrameRef.current);
|
|
}
|
|
function nextFrame(callback, delay = 2) {
|
|
cancelNextFrame();
|
|
const nextFrameId = raf(() => {
|
|
if (delay <= 1) {
|
|
callback({
|
|
isCanceled: () => nextFrameId !== nextFrameRef.current
|
|
});
|
|
} else {
|
|
nextFrame(callback, delay - 1);
|
|
}
|
|
});
|
|
nextFrameRef.current = nextFrameId;
|
|
}
|
|
React.useEffect(() => () => {
|
|
cancelNextFrame();
|
|
}, []);
|
|
return [nextFrame, cancelNextFrame];
|
|
}); |