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