@reservoir0x/relay-kit-ui
Version:
Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.
35 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const usehooks_ts_1 = require("usehooks-ts");
function useDebounceState(initialValue, delay, options) {
const memoOptions = (0, react_1.useMemo)(() => {
return options;
}, [options]);
const eq = memoOptions?.equalityFn ?? ((left, right) => left === right);
const unwrappedInitialValue = initialValue instanceof Function ? initialValue() : initialValue;
const [debouncedValue, setDebouncedValue] = (0, react_1.useState)(unwrappedInitialValue);
const [value, setValue] = (0, react_1.useState)(unwrappedInitialValue);
const previousValueRef = (0, react_1.useRef)(unwrappedInitialValue);
const updateDebouncedValue = (0, usehooks_ts_1.useDebounceCallback)(setDebouncedValue, delay, memoOptions);
if (!eq(previousValueRef.current, unwrappedInitialValue)) {
updateDebouncedValue(unwrappedInitialValue);
previousValueRef.current = unwrappedInitialValue;
}
return {
value,
debouncedValue,
setValue: (value) => {
updateDebouncedValue.cancel();
setValue(value);
updateDebouncedValue(value);
},
setDebouncedValue: (value) => {
updateDebouncedValue.cancel();
updateDebouncedValue(value);
},
debouncedControls: updateDebouncedValue
};
}
exports.default = useDebounceState;
//# sourceMappingURL=useDebounceState.js.map