33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
define(["require", "exports", "react", "./useConst"], function (require, exports, React, useConst_1) {
|
|
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.useSetInterval = void 0;
|
|
/**
|
|
* Returns a wrapper function for `setInterval` which automatically handles disposal.
|
|
*/
|
|
var useSetInterval = function () {
|
|
var intervalIds = (0, useConst_1.useConst)({});
|
|
React.useEffect(function () { return function () {
|
|
for (var _i = 0, _a = Object.keys(intervalIds); _i < _a.length; _i++) {
|
|
var id = _a[_i];
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
clearInterval(id);
|
|
}
|
|
}; },
|
|
// useConst ensures this will never change, but react-hooks/exhaustive-deps doesn't know that
|
|
[intervalIds]);
|
|
return (0, useConst_1.useConst)({
|
|
setInterval: function (func, duration) {
|
|
var id = setInterval(func, duration);
|
|
intervalIds[id] = 1;
|
|
return id;
|
|
},
|
|
clearInterval: function (id) {
|
|
delete intervalIds[id];
|
|
clearInterval(id);
|
|
},
|
|
});
|
|
};
|
|
exports.useSetInterval = useSetInterval;
|
|
});
|
|
//# sourceMappingURL=useSetInterval.js.map
|