@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
40 lines • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useStablePending = useStablePending;
const react_1 = require("react");
/**
* Returns a stabilized version of a "pending" boolean that delays the transition
* from true to false. Use when the raw pending flag toggles rapidly (e.g. during
* a single poll) and you want to avoid UI flicker.
*
* - When pending becomes true → returns true immediately.
* - When pending becomes false → returns false only after it has stayed false
* for `delayMs` (default 200ms). If pending goes true again before that, the
* delay is cancelled and the return value stays true.
*/
function useStablePending(pending, delayMs = 200) {
const [stablePending, setStablePending] = (0, react_1.useState)(pending);
const timeoutRef = (0, react_1.useRef)(null);
(0, react_1.useEffect)(() => {
if (pending) {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
setStablePending(true);
return;
}
if (timeoutRef.current)
clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(() => {
timeoutRef.current = null;
setStablePending(false);
}, delayMs);
return () => {
if (timeoutRef.current)
clearTimeout(timeoutRef.current);
};
}, [pending, delayMs]);
return stablePending;
}
//# sourceMappingURL=useStablePending.js.map