UNPKG

react-state-hooks

Version:
301 lines (264 loc) 9.66 kB
import {useState as $hgUW1$useState, useCallback as $hgUW1$useCallback, useEffect as $hgUW1$useEffect, useRef as $hgUW1$useRef, useMemo as $hgUW1$useMemo, useReducer as $hgUW1$useReducer} from "react"; function $421211bff816599e$export$2e2bcd8739ae039(getter, deps = []) { const [state, setState] = (0, $hgUW1$useState)(); const [error, setError] = (0, $hgUW1$useState)(); const [isPending, setIsPending] = (0, $hgUW1$useState)(true); const setData = (0, $hgUW1$useCallback)((value)=>{ setError(undefined); setIsPending(false); setState((current)=>value instanceof Function ? value(current) : value); }, []); const revalidate = (0, $hgUW1$useCallback)(async ()=>{ setIsPending(true); setError(undefined); try { setState(await getter()); } catch (err) { setState(undefined); setError(err); } setIsPending(false); }, deps); // Reset (0, $hgUW1$useEffect)(()=>{ revalidate(); }, [ revalidate ]); return [ state, setData, { error: error, isPending: isPending, revalidate: revalidate } ]; } function $f5ad044b61299761$export$2e2bcd8739ae039(initialValue, delay = 500) { const [state, _setState] = (0, $hgUW1$useState)(initialValue); const timeoutRef = (0, $hgUW1$useRef)(null); const setState = (0, $hgUW1$useCallback)((value)=>{ if (timeoutRef.current) clearTimeout(timeoutRef.current); timeoutRef.current = setTimeout(()=>{ _setState(value); }, delay); }, [ delay ]); return [ state, setState ]; } function $b1bf8d97f532f606$export$2e2bcd8739ae039(factory, deps = []) { const [state, setState] = (0, $hgUW1$useState)(factory(undefined)); (0, $hgUW1$useMemo)(()=>{ setState(factory); }, deps); return [ state, setState ]; } function $8f0a765cc5951d71$export$2e2bcd8739ae039(initialState, length = 10) { const historyRef = (0, $hgUW1$useRef)(typeof initialState === "undefined" ? [] : [ initialState ]); const [state, _setState] = (0, $hgUW1$useState)(initialState); const setState = (0, $hgUW1$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, $hgUW1$useCallback)((amount = 1)=>{ setState(historyRef.current.at(-(amount + 1))); }, [ historyRef, setState ]); return [ state, setState, { history: historyRef.current, rollback: rollback } ]; } function $7186655ef011381f$export$2e2bcd8739ae039(immutableValue) { const [state] = (0, $hgUW1$useState)(immutableValue); return state; } function $f257469bd5df4d90$export$2e2bcd8739ae039(initialState = []) { const [list, setList] = (0, $hgUW1$useState)(initialState); const actions = (0, $hgUW1$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 $ab09f87dc46e7c68$export$2e2bcd8739ae039(initialState, options = {}) { const resolveValue = (0, $hgUW1$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, $hgUW1$useState)(resolveValue(initialState)); const setState = (0, $hgUW1$useCallback)((value)=>{ _setState(resolveValue(value instanceof Function ? value(state) : value)); }, [ state, resolveValue ]); const inc = (0, $hgUW1$useCallback)((value = options.step ?? 1)=>{ setState((current)=>(current ?? 0) + value); }, [ setState, options.step ]); const dec = (0, $hgUW1$useCallback)((value = options.step ?? 1)=>{ setState((current)=>(current ?? 0) - value); }, [ setState, options.step ]); return [ state, setState, { inc: inc, dec: dec } ]; } function $3f50431da104d8de$export$2e2bcd8739ae039(initialState) { const [state, setState] = (0, $hgUW1$useState)(Object(initialState)); const updateState = (0, $hgUW1$useCallback)((value)=>{ setState((current)=>({ ...current, ...value })); }, []); const resetState = (0, $hgUW1$useCallback)((newValue)=>{ setState(Object(newValue)); }, []); return [ state, updateState, resetState ]; } function $cfb9341760f8207d$export$2e2bcd8739ae039(prop, initialState) { const [state, setState] = (0, $hgUW1$useState)(typeof prop !== "undefined" ? prop : initialState); (0, $hgUW1$useEffect)(()=>{ if (typeof prop !== "undefined") setState(prop); }, [ prop ]); return [ state, setState ]; } const $bd2908e8fd883616$var$store = {}; const $bd2908e8fd883616$var$listeners = {}; function $bd2908e8fd883616$export$2e2bcd8739ae039(key, initialState) { const [state, _setState] = (0, $hgUW1$useState)($bd2908e8fd883616$var$store[key] ?? initialState); const setState = (0, $hgUW1$useCallback)((value)=>{ const next = value instanceof Function ? value($bd2908e8fd883616$var$store[key]) : value; $bd2908e8fd883616$var$store[key] = next; $bd2908e8fd883616$var$listeners[key].forEach((listener)=>listener(next)); }, [ key ]); // #HACK onBeforeMount (0, $hgUW1$useMemo)(()=>{ // Store the initial state on the first call with this key $bd2908e8fd883616$var$store[key] = $bd2908e8fd883616$var$store[key] ?? initialState; // Create an empty array of listener on the first call with this key $bd2908e8fd883616$var$listeners[key] = $bd2908e8fd883616$var$listeners[key] ?? []; }, [ key ]); (0, $hgUW1$useEffect)(()=>{ // Register the observer const listener = (state)=>_setState(state); $bd2908e8fd883616$var$listeners[key].push(listener); // Cleanup when unmounting return ()=>{ const index = $bd2908e8fd883616$var$listeners[key].indexOf(listener); if (index > -1) $bd2908e8fd883616$var$listeners[key].splice(index, 1); }; }, [ key ]); return [ state, setState ]; } function $934cf9ded21aff0d$export$2e2bcd8739ae039(initialState = false) { return (0, $hgUW1$useReducer)((current)=>!current, initialState); } export {$421211bff816599e$export$2e2bcd8739ae039 as useAsyncState, $f5ad044b61299761$export$2e2bcd8739ae039 as useDebounceState, $b1bf8d97f532f606$export$2e2bcd8739ae039 as useDependentState, $8f0a765cc5951d71$export$2e2bcd8739ae039 as useHistoryState, $7186655ef011381f$export$2e2bcd8739ae039 as useImmutableState, $f257469bd5df4d90$export$2e2bcd8739ae039 as useListState, $ab09f87dc46e7c68$export$2e2bcd8739ae039 as useNumberState, $3f50431da104d8de$export$2e2bcd8739ae039 as useObjectState, $cfb9341760f8207d$export$2e2bcd8739ae039 as usePropState, $bd2908e8fd883616$export$2e2bcd8739ae039 as useStoreState, $934cf9ded21aff0d$export$2e2bcd8739ae039 as useToggleState}; //# sourceMappingURL=index.mjs.map