react-state-hooks
Version:
Collection of hooks to manage state.
316 lines (277 loc) • 10.2 kB
JavaScript
var $8zHUo$react = require("react");
function $parcel$export(e, n, v, s) {
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}
$parcel$export(module.exports, "useAsyncState", () => $10b6931eca1dac2b$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useDebounceState", () => $4f0237dff81fb1cd$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useDependentState", () => $9c7435a5c44a5997$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useHistoryState", () => $58891808d6af2c90$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useImmutableState", () => $353222d924cf8496$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useListState", () => $b2557a3ce2b7656b$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useNumberState", () => $1b1af5da21564c64$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useObjectState", () => $e03d311c7599e1ae$export$2e2bcd8739ae039);
$parcel$export(module.exports, "usePropState", () => $5fe28fc19fddeb7d$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useStoreState", () => $ab9cbd604fad9431$export$2e2bcd8739ae039);
$parcel$export(module.exports, "useToggleState", () => $8b4110013dd454a9$export$2e2bcd8739ae039);
function $10b6931eca1dac2b$export$2e2bcd8739ae039(getter, deps = []) {
const [state, setState] = (0, $8zHUo$react.useState)();
const [error, setError] = (0, $8zHUo$react.useState)();
const [isPending, setIsPending] = (0, $8zHUo$react.useState)(true);
const setData = (0, $8zHUo$react.useCallback)((value)=>{
setError(undefined);
setIsPending(false);
setState((current)=>value instanceof Function ? value(current) : value);
}, []);
const revalidate = (0, $8zHUo$react.useCallback)(async ()=>{
setIsPending(true);
setError(undefined);
try {
setState(await getter());
} catch (err) {
setState(undefined);
setError(err);
}
setIsPending(false);
}, deps);
// Reset
(0, $8zHUo$react.useEffect)(()=>{
revalidate();
}, [
revalidate
]);
return [
state,
setData,
{
error: error,
isPending: isPending,
revalidate: revalidate
}
];
}
function $4f0237dff81fb1cd$export$2e2bcd8739ae039(initialValue, delay = 500) {
const [state, _setState] = (0, $8zHUo$react.useState)(initialValue);
const timeoutRef = (0, $8zHUo$react.useRef)(null);
const setState = (0, $8zHUo$react.useCallback)((value)=>{
if (timeoutRef.current) clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(()=>{
_setState(value);
}, delay);
}, [
delay
]);
return [
state,
setState
];
}
function $9c7435a5c44a5997$export$2e2bcd8739ae039(factory, deps = []) {
const [state, setState] = (0, $8zHUo$react.useState)(factory(undefined));
(0, $8zHUo$react.useMemo)(()=>{
setState(factory);
}, deps);
return [
state,
setState
];
}
function $58891808d6af2c90$export$2e2bcd8739ae039(initialState, length = 10) {
const historyRef = (0, $8zHUo$react.useRef)(typeof initialState === "undefined" ? [] : [
initialState
]);
const [state, _setState] = (0, $8zHUo$react.useState)(initialState);
const setState = (0, $8zHUo$react.useCallback)((value)=>{
const next = value instanceof Function ? value(state) : value;
if (next !== historyRef.current.at(-1)) {
historyRef.current.push(next);
if (historyRef.current.length > length) {
const deleteCount = historyRef.current.length - length;
historyRef.current.splice(0, deleteCount);
}
}
_setState(next);
}, [
length,
state,
historyRef
]);
const rollback = (0, $8zHUo$react.useCallback)((amount = 1)=>{
setState(historyRef.current.at(-(amount + 1)));
}, [
historyRef,
setState
]);
return [
state,
setState,
{
history: historyRef.current,
rollback: rollback
}
];
}
function $353222d924cf8496$export$2e2bcd8739ae039(immutableValue) {
const [state] = (0, $8zHUo$react.useState)(immutableValue);
return state;
}
function $b2557a3ce2b7656b$export$2e2bcd8739ae039(initialState = []) {
const [list, setList] = (0, $8zHUo$react.useState)(initialState);
const actions = (0, $8zHUo$react.useMemo)(()=>({
set: (items)=>{
setList([
...items
]);
},
push: (...items)=>{
setList((prev)=>[
...prev,
...items
]);
},
insert: (index, item)=>{
setList((prev)=>{
const next = [
...prev
];
next.splice(index, 0, item);
return next;
});
},
remove: (indexOrHandler)=>{
setList((prev)=>{
return typeof indexOrHandler === "function" ? prev.filter((item, index)=>!indexOrHandler(item, index)) // Remove todas as ocorrências se for uma função
: prev.filter((_, i)=>i !== indexOrHandler); // Remove pelo índice
});
},
update: (indexOrHandler, newItem)=>{
setList((prev)=>{
return typeof indexOrHandler === "function" ? prev.map((item, index)=>indexOrHandler(item, index) ? newItem : item) // Atualiza baseado na função
: prev.map((item, index)=>index === indexOrHandler ? newItem : item); // Atualiza pelo índice
});
},
clear: ()=>{
setList([]);
},
sort: (compareFn)=>{
setList((prev)=>[
...prev
].sort(compareFn));
},
filter: (predicate)=>{
setList((prev)=>prev.filter(predicate));
}
}), []);
return [
list,
actions
];
}
function $1b1af5da21564c64$export$2e2bcd8739ae039(initialState, options = {}) {
const resolveValue = (0, $8zHUo$react.useCallback)((value)=>{
if (typeof value === "number") {
if (typeof options.min === "number") value = Math.max(options.min, value);
if (typeof options.max === "number") value = Math.min(options.max, value);
if (typeof options.step === "number") value = Math.floor(value / options.step) * options.step;
}
return value;
}, [
options
]);
const [state, _setState] = (0, $8zHUo$react.useState)(resolveValue(initialState));
const setState = (0, $8zHUo$react.useCallback)((value)=>{
_setState(resolveValue(value instanceof Function ? value(state) : value));
}, [
state,
resolveValue
]);
const inc = (0, $8zHUo$react.useCallback)((value = options.step ?? 1)=>{
setState((current)=>(current ?? 0) + value);
}, [
setState,
options.step
]);
const dec = (0, $8zHUo$react.useCallback)((value = options.step ?? 1)=>{
setState((current)=>(current ?? 0) - value);
}, [
setState,
options.step
]);
return [
state,
setState,
{
inc: inc,
dec: dec
}
];
}
function $e03d311c7599e1ae$export$2e2bcd8739ae039(initialState) {
const [state, setState] = (0, $8zHUo$react.useState)(Object(initialState));
const updateState = (0, $8zHUo$react.useCallback)((value)=>{
setState((current)=>({
...current,
...value
}));
}, []);
const resetState = (0, $8zHUo$react.useCallback)((newValue)=>{
setState(Object(newValue));
}, []);
return [
state,
updateState,
resetState
];
}
function $5fe28fc19fddeb7d$export$2e2bcd8739ae039(prop, initialState) {
const [state, setState] = (0, $8zHUo$react.useState)(typeof prop !== "undefined" ? prop : initialState);
(0, $8zHUo$react.useEffect)(()=>{
if (typeof prop !== "undefined") setState(prop);
}, [
prop
]);
return [
state,
setState
];
}
const $ab9cbd604fad9431$var$store = {};
const $ab9cbd604fad9431$var$listeners = {};
function $ab9cbd604fad9431$export$2e2bcd8739ae039(key, initialState) {
const [state, _setState] = (0, $8zHUo$react.useState)($ab9cbd604fad9431$var$store[key] ?? initialState);
const setState = (0, $8zHUo$react.useCallback)((value)=>{
const next = value instanceof Function ? value($ab9cbd604fad9431$var$store[key]) : value;
$ab9cbd604fad9431$var$store[key] = next;
$ab9cbd604fad9431$var$listeners[key].forEach((listener)=>listener(next));
}, [
key
]);
// #HACK onBeforeMount
(0, $8zHUo$react.useMemo)(()=>{
// Store the initial state on the first call with this key
$ab9cbd604fad9431$var$store[key] = $ab9cbd604fad9431$var$store[key] ?? initialState;
// Create an empty array of listener on the first call with this key
$ab9cbd604fad9431$var$listeners[key] = $ab9cbd604fad9431$var$listeners[key] ?? [];
}, [
key
]);
(0, $8zHUo$react.useEffect)(()=>{
// Register the observer
const listener = (state)=>_setState(state);
$ab9cbd604fad9431$var$listeners[key].push(listener);
// Cleanup when unmounting
return ()=>{
const index = $ab9cbd604fad9431$var$listeners[key].indexOf(listener);
if (index > -1) $ab9cbd604fad9431$var$listeners[key].splice(index, 1);
};
}, [
key
]);
return [
state,
setState
];
}
function $8b4110013dd454a9$export$2e2bcd8739ae039(initialState = false) {
return (0, $8zHUo$react.useReducer)((current)=>!current, initialState);
}
//# sourceMappingURL=index.js.map