From ddc18557b5de23fee2e0723b305fd8d8de62bd17 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 3 Sep 2023 18:25:03 -0700 Subject: feat(settings): move cache minutes to settings --- src/stores/settings.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'src/stores/settings.ts') diff --git a/src/stores/settings.ts b/src/stores/settings.ts index 30f1c34d..9396ec74 100644 --- a/src/stores/settings.ts +++ b/src/stores/settings.ts @@ -2,8 +2,8 @@ import { browser } from '$app/environment'; import { writable } from 'svelte/store'; interface Settings { - // cacheMangaMinutes: number; - // cacheMinutes: number; + cacheMangaMinutes: number; + cacheMinutes: number; closeAnimeByDefault: boolean; closeMangaByDefault: boolean; displayNotStarted: boolean; @@ -13,8 +13,8 @@ interface Settings { } const defaultSettings: Settings = { - // cacheMangaMinutes: 60, - // cacheMinutes: 30, + cacheMangaMinutes: 60, + cacheMinutes: 30, closeAnimeByDefault: false, closeMangaByDefault: false, displayNotStarted: false, @@ -24,7 +24,16 @@ const defaultSettings: Settings = { }; const createStore = () => { - const { subscribe, set, update } = writable(defaultSettings); + const { subscribe, set, update } = writable( + JSON.parse( + browser + ? localStorage.getItem('settings') ?? JSON.stringify(defaultSettings) + : JSON.stringify(defaultSettings) + ) + ); + let state: Settings; + + subscribe((value) => (state = value)); return { subscribe, @@ -32,13 +41,8 @@ const createStore = () => { update, reset: () => set(defaultSettings), get: () => { - const settings = JSON.parse( - browser - ? localStorage.getItem('settings') ?? JSON.stringify(defaultSettings) - : JSON.stringify(defaultSettings) - ); const keys = Object.keys(defaultSettings); - const settingsKeys = Object.keys(settings); + const settingsKeys = Object.keys(state); if (keys.length !== settingsKeys.length) { return defaultSettings; @@ -50,9 +54,9 @@ const createStore = () => { } } - return settings; + return state; }, - setKey: (key: keyof Settings, value: any) => + setKey: (key: keyof Settings, value: unknown) => update((settings) => ({ ...settings, [key]: value })) }; }; -- cgit v1.2.3