mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-23 00:45:16 +00:00
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
exports.default = void 0;
|
||
|
|
||
|
var _react = require("react");
|
||
|
|
||
|
var _useCommittedRef = _interopRequireDefault(require("./useCommittedRef"));
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
function useInterval(fn, ms, paused, runImmediately) {
|
||
|
if (paused === void 0) {
|
||
|
paused = false;
|
||
|
}
|
||
|
|
||
|
if (runImmediately === void 0) {
|
||
|
runImmediately = false;
|
||
|
}
|
||
|
|
||
|
var handle;
|
||
|
var fnRef = (0, _useCommittedRef.default)(fn); // this ref is necessary b/c useEffect will sometimes miss a paused toggle
|
||
|
// orphaning a setTimeout chain in the aether, so relying on it's refresh logic is not reliable.
|
||
|
|
||
|
var pausedRef = (0, _useCommittedRef.default)(paused);
|
||
|
|
||
|
var tick = function tick() {
|
||
|
if (pausedRef.current) return;
|
||
|
fnRef.current();
|
||
|
schedule(); // eslint-disable-line no-use-before-define
|
||
|
};
|
||
|
|
||
|
var schedule = function schedule() {
|
||
|
clearTimeout(handle);
|
||
|
handle = setTimeout(tick, ms);
|
||
|
};
|
||
|
|
||
|
(0, _react.useEffect)(function () {
|
||
|
if (runImmediately) {
|
||
|
tick();
|
||
|
} else {
|
||
|
schedule();
|
||
|
}
|
||
|
|
||
|
return function () {
|
||
|
return clearTimeout(handle);
|
||
|
};
|
||
|
}, [paused, runImmediately]);
|
||
|
}
|
||
|
|
||
|
var _default = useInterval;
|
||
|
exports.default = _default;
|