30 lines
819 B
JavaScript
30 lines
819 B
JavaScript
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];
|
|
}); |