react-tidy
Version:
A collection of atomic, independent, typed, tested and documented React custom hooks
224 lines (175 loc) • 7.56 kB
JavaScript
import $hgUW1$react from "react";
function $parcel$exportWildcard(dest, source) {
Object.keys(source).forEach(function(key) {
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
return;
}
Object.defineProperty(dest, key, {
enumerable: true,
get: function get() {
return source[key];
}
});
});
return dest;
}
function $parcel$export(e, n, v, s) {
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}
var $3beff1f3cd4ed032$exports = {};
var $ea09ef34cd06c77e$exports = {};
$parcel$export($ea09ef34cd06c77e$exports, "useStorage", () => $ea09ef34cd06c77e$export$95809329f49ea9f9);
function $efabb5aef85c726c$export$f6e2535fb5126e54(x) {
return !!x && ({}).toString.call(x) === "[object Function]";
}
function $8c6b22a6abbf00f3$export$23b68da80b41ce8c() {
return typeof window === "object" && typeof document === "object";
}
var $c6ed51dc5680bb65$exports = {};
$parcel$export($c6ed51dc5680bb65$exports, "getDefaultStorage", () => $c6ed51dc5680bb65$export$c164ff19f266df37);
$parcel$export($c6ed51dc5680bb65$exports, "setDefaultStorage", () => $c6ed51dc5680bb65$export$ed3bf0d4310b827);
var $4ef095d558b4371b$exports = {};
$parcel$export($4ef095d558b4371b$exports, "createMemoryStorage", () => $4ef095d558b4371b$export$511bcabbec3c7baa);
const $4ef095d558b4371b$export$511bcabbec3c7baa = ()=>{
return new $4ef095d558b4371b$var$MemoryStorage();
};
class $4ef095d558b4371b$var$MemoryStorage {
memory = {};
get length() {
return Object.keys(this.memory).length;
}
getItem(key) {
return this.memory[key] || null;
}
setItem(key, value) {
this.memory[key] = value;
}
removeItem(key) {
delete this.memory[key];
}
key(index) {
const keys = Object.keys(this.memory);
return keys[index] || null;
}
clear() {
this.memory = {};
}
}
let $c6ed51dc5680bb65$var$defaultStorage;
if ((0, $8c6b22a6abbf00f3$export$23b68da80b41ce8c)()) $c6ed51dc5680bb65$var$defaultStorage = window.localStorage;
else $c6ed51dc5680bb65$var$defaultStorage = (0, $4ef095d558b4371b$export$511bcabbec3c7baa)();
function $c6ed51dc5680bb65$export$c164ff19f266df37() {
return $c6ed51dc5680bb65$var$defaultStorage;
}
function $c6ed51dc5680bb65$export$ed3bf0d4310b827(storage) {
$c6ed51dc5680bb65$var$defaultStorage = storage;
}
var $7d3ba43d00f22540$exports = {};
$parcel$export($7d3ba43d00f22540$exports, "useIsMounted", () => $7d3ba43d00f22540$export$80c2a3957bcb42ea);
function $7d3ba43d00f22540$export$80c2a3957bcb42ea() {
const ref = (0, $hgUW1$react).useRef(true);
(0, $hgUW1$react).useEffect(()=>{
return ()=>{
ref.current = false;
};
}, []);
return (0, $hgUW1$react).useCallback(()=>ref.current, []);
}
function $ea09ef34cd06c77e$export$95809329f49ea9f9(key, defaultValue = null, getStorage = (0, $c6ed51dc5680bb65$export$c164ff19f266df37)()) {
const isMounted = (0, $7d3ba43d00f22540$export$80c2a3957bcb42ea)();
const storage = (0, $hgUW1$react).useMemo(()=>{
if (!(0, $8c6b22a6abbf00f3$export$23b68da80b41ce8c)()) return (0, $4ef095d558b4371b$export$511bcabbec3c7baa)();
if ((0, $efabb5aef85c726c$export$f6e2535fb5126e54)(getStorage)) return getStorage();
return getStorage;
}, [
getStorage
]);
const initialValue = (0, $hgUW1$react).useMemo(()=>{
const storedString = storage.getItem(key);
return storedString === null ? defaultValue : JSON.parse(storedString);
}, [
storage,
key,
defaultValue
]);
const [value, setValue] = (0, $hgUW1$react).useState(initialValue);
(0, $hgUW1$react).useEffect(()=>{
storage.setItem(key, JSON.stringify(initialValue));
}, [
key,
initialValue
]);
const setItem = (0, $hgUW1$react).useCallback((newValue)=>{
if ((0, $efabb5aef85c726c$export$f6e2535fb5126e54)(newValue)) newValue = newValue(value);
if (newValue === null) storage.removeItem(key);
else storage.setItem(key, JSON.stringify(newValue));
if (isMounted()) setValue(newValue);
}, [
storage,
key,
value
]);
return [
value,
setItem
];
}
$parcel$exportWildcard($3beff1f3cd4ed032$exports, $ea09ef34cd06c77e$exports);
$parcel$exportWildcard($3beff1f3cd4ed032$exports, $c6ed51dc5680bb65$exports);
$parcel$exportWildcard($3beff1f3cd4ed032$exports, $4ef095d558b4371b$exports);
var $abf78004b7e85424$exports = {};
$parcel$export($abf78004b7e85424$exports, "useRefresh", () => $abf78004b7e85424$export$51fab111ee90cd8d);
function $abf78004b7e85424$export$51fab111ee90cd8d() {
const [, setVersion] = (0, $hgUW1$react).useState(0);
const isMounted = (0, $7d3ba43d00f22540$export$80c2a3957bcb42ea)();
return ()=>{
if (isMounted()) setVersion((x)=>x + 1);
};
}
var $8a40fdc1e5ac5117$exports = {};
$parcel$export($8a40fdc1e5ac5117$exports, "useInstance", () => $8a40fdc1e5ac5117$export$f4a5c5dcfe1bba82);
const $84a7f9cc6f95309f$var$proxySymbol = Symbol();
function $84a7f9cc6f95309f$export$ce549044b8a3907f(target, notify) {
if (target[$84a7f9cc6f95309f$var$proxySymbol] === true) return target;
return new Proxy(target, {
get: $84a7f9cc6f95309f$var$makeTrap("get", notify),
defineProperty: $84a7f9cc6f95309f$var$makeTrap("defineProperty", notify),
deleteProperty: $84a7f9cc6f95309f$var$makeTrap("deleteProperty", notify)
});
}
function $84a7f9cc6f95309f$var$makeTrap(trap, notify) {
return (...args)=>{
if (trap === "get" && args[1] === $84a7f9cc6f95309f$var$proxySymbol) return true;
const original = Reflect[trap];
let result = original(...args);
if (trap === "get" && typeof result === "object" && result !== null) result = $84a7f9cc6f95309f$export$ce549044b8a3907f(result, notify);
if (trap !== "get") notify();
return result;
};
}
function $8a40fdc1e5ac5117$export$f4a5c5dcfe1bba82(Class, ...args) {
const refresh = (0, $abf78004b7e85424$export$51fab111ee90cd8d)();
const instance = (0, $hgUW1$react).useMemo(()=>(0, $84a7f9cc6f95309f$export$ce549044b8a3907f)(new Class(...args), refresh), args);
(0, $hgUW1$react).useEffect(()=>{
const currentInstance = instance;
return ()=>currentInstance.cleanup && currentInstance.cleanup();
}, args);
return instance;
}
var $1070e24ced0b957e$exports = {};
$parcel$export($1070e24ced0b957e$exports, "usePrevious", () => $1070e24ced0b957e$export$5cae361ad82dce8b);
function $1070e24ced0b957e$export$5cae361ad82dce8b(value, count = 1) {
const [history, setHistory] = (0, $hgUW1$react).useState([]);
(0, $hgUW1$react).useEffect(()=>{
if (history.length > count) history.pop();
setHistory([
value,
...history
]);
}, [
value
]);
return history.slice(1);
}
export {$ea09ef34cd06c77e$export$95809329f49ea9f9 as useStorage, $c6ed51dc5680bb65$export$c164ff19f266df37 as getDefaultStorage, $c6ed51dc5680bb65$export$ed3bf0d4310b827 as setDefaultStorage, $4ef095d558b4371b$export$511bcabbec3c7baa as createMemoryStorage, $abf78004b7e85424$export$51fab111ee90cd8d as useRefresh, $8a40fdc1e5ac5117$export$f4a5c5dcfe1bba82 as useInstance, $1070e24ced0b957e$export$5cae361ad82dce8b as usePrevious, $7d3ba43d00f22540$export$80c2a3957bcb42ea as useIsMounted};
//# sourceMappingURL=react-tidy.mjs.map