1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = void 0;
|
5 | var _react = require("react");
|
6 | var _useCommittedRef = _interopRequireDefault(require("./useCommittedRef"));
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
8 | function useRafInterval(fn, ms, paused = false) {
|
9 | let handle;
|
10 | let start = new Date().getTime();
|
11 | const fnRef = (0, _useCommittedRef.default)(fn);
|
12 |
|
13 |
|
14 | const pausedRef = (0, _useCommittedRef.default)(paused);
|
15 | function loop() {
|
16 | const current = new Date().getTime();
|
17 | const delta = current - start;
|
18 | if (pausedRef.current) return;
|
19 | if (delta >= ms && fnRef.current) {
|
20 | fnRef.current();
|
21 | start = new Date().getTime();
|
22 | }
|
23 | cancelAnimationFrame(handle);
|
24 | handle = requestAnimationFrame(loop);
|
25 | }
|
26 | (0, _react.useEffect)(() => {
|
27 | handle = requestAnimationFrame(loop);
|
28 | return () => cancelAnimationFrame(handle);
|
29 | }, []);
|
30 | }
|
31 | var _default = useRafInterval;
|
32 | exports.default = _default; |
\ | No newline at end of file |