import { writable, type Writable } from "svelte/store"; function isStore(v: unknown): v is Writable { return v !== null && typeof v === "object" && "subscribe" in v && "set" in v; } const storeIds: Map> = new Map(); const HAS_LOCALSTORAGE = "localStorage" in globalThis; function createLocalStore(key: string, initialValue: T | Writable) { let store: Writable; if (HAS_LOCALSTORAGE) { const localValue = localStorage.getItem(key); const value = localValue ? JSON.parse(localValue) : null; if (value === null) { if (isStore(initialValue)) { store = initialValue; } else { store = writable(initialValue); } } else { store = writable(value); } } else { return isStore(initialValue) ? initialValue : writable(initialValue); } store.subscribe((value) => { localStorage.setItem(key, JSON.stringify(value)); }); return { subscribe: store.subscribe, set: store.set, update: store.update } } export default function localStore(key: string, initialValue: T | Writable): Writable { if (storeIds.has(key)) return storeIds.get(key) as Writable; const store = createLocalStore(key, initialValue) storeIds.set(key, store); return store }