feats
Version:
A comprehensive TypeScript utility library featuring fluent text building, type-safe switching, duration utilities, React hooks, and extended array/object prototypes for modern JavaScript development.
72 lines (67 loc) • 2.21 kB
JavaScript
;
var react = require('react');
/**
* React hook for managing a single timeout with manual control.
* Automatically clears the timeout on component unmount.
*/
function useTimeout() {
const timeoutRef = react.useRef(null);
react.useEffect(() => {
return clear;
}, []);
const handle = react.useCallback(() => {
var _a;
(_a = timeoutRef.current) === null || _a === void 0 ? void 0 : _a.method();
timeoutRef.current = null;
}, []);
const clear = react.useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current.id);
timeoutRef.current = null;
}
}, []);
const set = react.useCallback((handler, time) => {
clear();
const id = setTimeout(handle, time);
timeoutRef.current = {
id,
method: handler,
};
}, [clear, handle]);
const dispatch = react.useCallback(() => {
var _a;
const method = (_a = timeoutRef.current) === null || _a === void 0 ? void 0 : _a.method;
clear();
method === null || method === void 0 ? void 0 : method();
}, [clear]);
return react.useMemo(() => ({ set, clear, dispatch }), [set, clear, dispatch]);
}
/**
* React hook for managing an interval with manual control.
* Automatically clears the interval on component unmount.
*/
function useInterval() {
const intervalRef = react.useRef(null);
react.useEffect(() => clear, []);
const clear = react.useCallback(() => {
if (intervalRef.current) {
clearInterval(intervalRef.current.id);
intervalRef.current = null;
}
}, []);
const set = react.useCallback((handler, interval) => {
clear();
const id = setInterval(() => {
var _a;
(_a = intervalRef.current) === null || _a === void 0 ? void 0 : _a.method();
}, interval);
intervalRef.current = {
id,
method: handler,
};
}, [clear]);
return react.useMemo(() => ({ set, clear }), [set, clear]);
}
exports.useInterval = useInterval;
exports.useTimeout = useTimeout;
//# sourceMappingURL=react.cjs.map