첫 커밋
This commit is contained in:
119
.vite/deps/use-local-storage-state.js
Normal file
119
.vite/deps/use-local-storage-state.js
Normal file
@@ -0,0 +1,119 @@
|
||||
import {
|
||||
require_react
|
||||
} from "./chunk-R7JHQV4C.js";
|
||||
import {
|
||||
__toESM
|
||||
} from "./chunk-PLDDJCW6.js";
|
||||
|
||||
// node_modules/.pnpm/use-local-storage-state@19._4d94ea5c8b720894eddc04bd1d07d188/node_modules/use-local-storage-state/src/useLocalStorageState.js
|
||||
var import_react = __toESM(require_react(), 1);
|
||||
var inMemoryData = /* @__PURE__ */ new Map();
|
||||
function useLocalStorageState(key, options) {
|
||||
const serializer = options?.serializer;
|
||||
const [defaultValue] = (0, import_react.useState)(options?.defaultValue);
|
||||
const [defaultServerValue] = (0, import_react.useState)(options?.defaultServerValue);
|
||||
return useLocalStorage(key, defaultValue, defaultServerValue, options?.storageSync, serializer?.parse, serializer?.stringify);
|
||||
}
|
||||
function useLocalStorage(key, defaultValue, defaultServerValue, storageSync = true, parse = parseJSON, stringify = JSON.stringify) {
|
||||
const storageItem = (0, import_react.useRef)({
|
||||
string: null,
|
||||
parsed: void 0
|
||||
});
|
||||
const value = (0, import_react.useSyncExternalStore)(
|
||||
// useSyncExternalStore.subscribe
|
||||
(0, import_react.useCallback)((onStoreChange) => {
|
||||
const onChange = (localKey) => {
|
||||
if (key === localKey) {
|
||||
onStoreChange();
|
||||
}
|
||||
};
|
||||
callbacks.add(onChange);
|
||||
return () => {
|
||||
callbacks.delete(onChange);
|
||||
};
|
||||
}, [key]),
|
||||
// useSyncExternalStore.getSnapshot
|
||||
() => {
|
||||
const string = goodTry(() => localStorage.getItem(key)) ?? null;
|
||||
if (inMemoryData.has(key)) {
|
||||
storageItem.current.parsed = inMemoryData.get(key);
|
||||
} else if (string !== storageItem.current.string) {
|
||||
let parsed;
|
||||
try {
|
||||
parsed = string === null ? defaultValue : parse(string);
|
||||
} catch {
|
||||
parsed = defaultValue;
|
||||
}
|
||||
storageItem.current.parsed = parsed;
|
||||
}
|
||||
storageItem.current.string = string;
|
||||
if (defaultValue !== void 0 && string === null) {
|
||||
goodTry(() => {
|
||||
const string2 = stringify(defaultValue);
|
||||
localStorage.setItem(key, string2);
|
||||
storageItem.current = { string: string2, parsed: defaultValue };
|
||||
});
|
||||
}
|
||||
return storageItem.current.parsed;
|
||||
},
|
||||
// useSyncExternalStore.getServerSnapshot
|
||||
() => defaultServerValue ?? defaultValue
|
||||
);
|
||||
const setState = (0, import_react.useCallback)((newValue) => {
|
||||
const value2 = newValue instanceof Function ? newValue(storageItem.current.parsed) : newValue;
|
||||
try {
|
||||
localStorage.setItem(key, stringify(value2));
|
||||
inMemoryData.delete(key);
|
||||
} catch {
|
||||
inMemoryData.set(key, value2);
|
||||
}
|
||||
triggerCallbacks(key);
|
||||
}, [key, stringify]);
|
||||
const removeItem = (0, import_react.useCallback)(() => {
|
||||
goodTry(() => localStorage.removeItem(key));
|
||||
inMemoryData.delete(key);
|
||||
triggerCallbacks(key);
|
||||
}, [key]);
|
||||
(0, import_react.useEffect)(() => {
|
||||
if (!storageSync) {
|
||||
return void 0;
|
||||
}
|
||||
const onStorage = (e) => {
|
||||
if (e.key === key && e.storageArea === goodTry(() => localStorage)) {
|
||||
triggerCallbacks(key);
|
||||
}
|
||||
};
|
||||
window.addEventListener("storage", onStorage);
|
||||
return () => window.removeEventListener("storage", onStorage);
|
||||
}, [key, storageSync]);
|
||||
return (0, import_react.useMemo)(() => [
|
||||
value,
|
||||
setState,
|
||||
{
|
||||
isPersistent: value === defaultValue || !inMemoryData.has(key),
|
||||
removeItem
|
||||
}
|
||||
], [key, setState, value, defaultValue, removeItem]);
|
||||
}
|
||||
var callbacks = /* @__PURE__ */ new Set();
|
||||
function triggerCallbacks(key) {
|
||||
for (const callback of [...callbacks]) {
|
||||
callback(key);
|
||||
}
|
||||
}
|
||||
function parseJSON(value) {
|
||||
return value === "undefined" ? void 0 : JSON.parse(value);
|
||||
}
|
||||
function goodTry(tryFn) {
|
||||
try {
|
||||
return tryFn();
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
|
||||
// node_modules/.pnpm/use-local-storage-state@19._4d94ea5c8b720894eddc04bd1d07d188/node_modules/use-local-storage-state/index.js
|
||||
var use_local_storage_state_default = useLocalStorageState;
|
||||
export {
|
||||
use_local_storage_state_default as default
|
||||
};
|
||||
//# sourceMappingURL=use-local-storage-state.js.map
|
||||
Reference in New Issue
Block a user