import { browser } from '$app/environment'; import { writable } from 'svelte/store'; interface LastPruneTimes { anime: number; chapters: number; manga: number; } const defaultTimes: LastPruneTimes = { anime: 1, chapters: 1, manga: 1 }; const createStore = () => { const { subscribe, set, update } = writable( JSON.parse( browser ? localStorage.getItem('lastPruneTimes') ?? JSON.stringify(defaultTimes) : JSON.stringify(defaultTimes) ) ); let state: LastPruneTimes; subscribe((value) => (state = value)); return { subscribe, set, update, reset: () => set(defaultTimes), get: () => { const keys = Object.keys(defaultTimes); const lastPruneTimesKeys = Object.keys(state); if (keys.length !== lastPruneTimesKeys.length) { return defaultTimes; } for (const key of keys) { if (!lastPruneTimesKeys.includes(key)) { return defaultTimes; } } return state; }, setKey: (key: keyof LastPruneTimes, value: unknown) => update((lastPruneTimes) => ({ ...lastPruneTimes, [key]: value })) }; }; const lastPruneTimes = createStore(); lastPruneTimes.subscribe((value) => { if (browser) { localStorage.setItem('lastPruneTimes', JSON.stringify(value)); } }); export default lastPruneTimes;