diff options
Diffstat (limited to 'src/stores')
| -rw-r--r-- | src/stores/announcementHash.ts | 17 | ||||
| -rw-r--r-- | src/stores/identity.ts | 43 | ||||
| -rw-r--r-- | src/stores/lastPruneTimes.ts | 46 | ||||
| -rw-r--r-- | src/stores/settings.ts | 61 | ||||
| -rw-r--r-- | src/stores/stateBin.ts | 15 |
5 files changed, 101 insertions, 81 deletions
diff --git a/src/stores/announcementHash.ts b/src/stores/announcementHash.ts index 0ff57b3b..d14c305e 100644 --- a/src/stores/announcementHash.ts +++ b/src/stores/announcementHash.ts @@ -1,12 +1,17 @@ import { browser } from '$app/environment'; import { writable } from 'svelte/store'; +import localforage from 'localforage'; -const announcementHash = writable<number>( - browser ? parseInt(localStorage.getItem('announcementHash') || '1') : 0 -); +const announcementHash = writable<number>(1); -announcementHash.subscribe((value) => { - if (browser) localStorage.setItem('announcementHash', value.toString()); -}); +if (browser) { + localforage.getItem<number>('announcementHash').then((value) => { + if (typeof value === 'number') announcementHash.set(value); + }); + + announcementHash.subscribe((value) => { + localforage.setItem('announcementHash', value); + }); +} export default announcementHash; diff --git a/src/stores/identity.ts b/src/stores/identity.ts index 2618d744..596c7176 100644 --- a/src/stores/identity.ts +++ b/src/stores/identity.ts @@ -1,6 +1,7 @@ import { browser } from '$app/environment'; import type { UserIdentity } from '$lib/Data/AniList/identity'; import { writable } from 'svelte/store'; +import localforage from 'localforage'; export const defaultIdentity: UserIdentity = { name: '', @@ -9,22 +10,26 @@ export const defaultIdentity: UserIdentity = { }; const createStore = () => { - const { subscribe, set, update } = writable<UserIdentity>( - JSON.parse( - browser - ? (localStorage.getItem('identity') ?? JSON.stringify(defaultIdentity)) - : JSON.stringify(defaultIdentity) - ) - ); - let state: UserIdentity; + const store = writable<UserIdentity>(defaultIdentity); + let state: UserIdentity = defaultIdentity; - subscribe((value) => (state = value)); + if (browser) + localforage.getItem<UserIdentity>('identity').then((value) => { + if (value && typeof value === 'object') store.set(value); + }); + + store.subscribe((value) => { + state = value; + + if (browser) localforage.setItem('identity', value); + }); return { - subscribe, - set, - update, - reset: () => set(defaultIdentity), + subscribe: store.subscribe, + set: store.set, + update: store.update, + reset: () => store.set(defaultIdentity), + get: () => { const keys = Object.keys(defaultIdentity); const identityKeys = Object.keys(state); @@ -32,22 +37,18 @@ const createStore = () => { for (const key of keys) if (!identityKeys.includes(key)) - (updatedIdentity[key as keyof UserIdentity] as unknown) = - defaultIdentity[key as keyof UserIdentity]; + updatedIdentity[key as keyof UserIdentity] = defaultIdentity[key as keyof UserIdentity]; - if (browser) localStorage.setItem('identity', JSON.stringify(updatedIdentity)); + if (browser) localforage.setItem('identity', updatedIdentity); return updatedIdentity; }, + setKey: (key: keyof UserIdentity, value: unknown) => - update((identity) => ({ ...identity, [key]: value })) + store.update((identity) => ({ ...identity, [key]: value })) }; }; const identity = createStore(); -identity.subscribe((value) => { - if (browser) localStorage.setItem('identity', JSON.stringify(value)); -}); - export default identity; diff --git a/src/stores/lastPruneTimes.ts b/src/stores/lastPruneTimes.ts index 9c4afc5b..212f2bc9 100644 --- a/src/stores/lastPruneTimes.ts +++ b/src/stores/lastPruneTimes.ts @@ -1,5 +1,6 @@ import { browser } from '$app/environment'; import { writable } from 'svelte/store'; +import localforage from 'localforage'; interface LastPruneTimes { anime: number; @@ -14,41 +15,42 @@ const defaultTimes: LastPruneTimes = { }; const createStore = () => { - const { subscribe, set, update } = writable<LastPruneTimes>( - JSON.parse( - browser - ? (localStorage.getItem('lastPruneTimes') ?? JSON.stringify(defaultTimes)) - : JSON.stringify(defaultTimes) - ) - ); - let state: LastPruneTimes; + const store = writable<LastPruneTimes>(defaultTimes); + let state: LastPruneTimes = defaultTimes; - subscribe((value) => (state = value)); + if (browser) + localforage.getItem<LastPruneTimes>('lastPruneTimes').then((value) => { + if (value && Object.keys(value).length === Object.keys(defaultTimes).length) store.set(value); + }); + + store.subscribe((value) => { + state = value; + + if (browser) localforage.setItem('lastPruneTimes', value); + }); return { - subscribe, - set, - update, - reset: () => set(defaultTimes), + subscribe: store.subscribe, + set: store.set, + update: store.update, + reset: () => store.set(defaultTimes), + get: () => { const keys = Object.keys(defaultTimes); - const lastPruneTimesKeys = Object.keys(state); + const stateKeys = Object.keys(state); - if (keys.length !== lastPruneTimesKeys.length) return defaultTimes; + if (keys.length !== stateKeys.length) return defaultTimes; - for (const key of keys) if (!lastPruneTimesKeys.includes(key)) return defaultTimes; + for (const key of keys) if (!stateKeys.includes(key)) return defaultTimes; return state; }, - setKey: (key: keyof LastPruneTimes, value: unknown) => - update((lastPruneTimes) => ({ ...lastPruneTimes, [key]: value })) + + setKey: (key: keyof LastPruneTimes, value: number) => + store.update((times) => ({ ...times, [key]: value })) }; }; const lastPruneTimes = createStore(); -lastPruneTimes.subscribe((value) => { - if (browser) localStorage.setItem('lastPruneTimes', JSON.stringify(value)); -}); - export default lastPruneTimes; diff --git a/src/stores/settings.ts b/src/stores/settings.ts index fef0debf..c8ae9c94 100644 --- a/src/stores/settings.ts +++ b/src/stores/settings.ts @@ -4,6 +4,7 @@ import { get, writable } from 'svelte/store'; import settingsSyncPulled from './settingsSyncPulled'; import settingsSyncTimes from './settingsSyncTimes'; import identity from './identity'; +import localforage from 'localforage'; const VERSION = '1.0.1'; @@ -135,22 +136,26 @@ const defaultSettings: Settings = { }; const createStore = () => { - const { subscribe, set, update } = writable<Settings>( - JSON.parse( - browser - ? (localStorage.getItem('settings') ?? JSON.stringify(defaultSettings)) - : JSON.stringify(defaultSettings) - ) - ); - let state: Settings; + const store = writable<Settings>(defaultSettings); + let state: Settings = defaultSettings; - subscribe((value) => (state = value)); + if (browser) + localforage.getItem<Settings>('settings').then((value) => { + if (value && typeof value === 'object') store.set(value); + }); + + store.subscribe((value) => { + state = value; + + if (browser) localforage.setItem('settings', value); + }); return { - subscribe, - set, - update, - reset: () => set(defaultSettings), + subscribe: store.subscribe, + set: store.set, + update: store.update, + reset: () => store.set(defaultSettings), + get: () => { const keys = Object.keys(defaultSettings); const settingsKeys = Object.keys(state); @@ -158,51 +163,53 @@ const createStore = () => { for (const key of keys) if (!settingsKeys.includes(key)) - (updatedSettings[key as keyof Settings] as unknown) = - defaultSettings[key as keyof Settings]; + updatedSettings[key as keyof Settings] = defaultSettings[key as keyof Settings]; - if (browser) localStorage.setItem('settings', JSON.stringify(updatedSettings)); + if (browser) localforage.setItem('settings', updatedSettings); return updatedSettings; }, + setKey: (key: keyof Settings, value: unknown) => - update((settings) => ({ ...settings, [key]: value })) + store.update((settings) => ({ ...settings, [key]: value })) }; }; const settings = createStore(); settings.subscribe((value) => { - if (browser) localStorage.setItem('settings', JSON.stringify(value)); + if (!browser) return; - if (value.settingsSync && get(settingsSyncPulled) == true) + if (value.settingsSync && get(settingsSyncPulled) === true) { fetch(root(`/api/configuration?id=${get(identity).id}`)).then((response) => { if (response.ok) response.json().then((data) => { - const isEqualsJson = (object1: Settings, object2: Settings) => { + const isEqualsJson = (firstObject: Settings, secondObject: Settings) => { type AnyObject = { [key: string]: unknown }; return ( - Object.keys(object1).length === Object.keys(object2).length && - Object.keys(object1).every( - (key) => - (object1 as unknown as AnyObject)[key] == (object2 as unknown as AnyObject)[key] + Object.keys(firstObject).length === Object.keys(secondObject).length && + Object.keys(firstObject).every( + (key) => (firstObject as AnyObject)[key] === (secondObject as AnyObject)[key] ) ); }; - if (data && data.configuration && !isEqualsJson(data.configuration, value)) { + if (data?.configuration && !isEqualsJson(data.configuration, value)) fetch(root(`/api/configuration`), { method: 'PUT', body: JSON.stringify(value) }).then((response) => { if (response.ok) console.log('Pushed local configuration'); - settingsSyncTimes.update((times) => ({ ...times, lastPush: new Date() })); + settingsSyncTimes.update((times) => ({ + ...times, + lastPush: new Date() + })); }); - } }); }); + } }); export default settings; diff --git a/src/stores/stateBin.ts b/src/stores/stateBin.ts index 74ea510f..082a07dc 100644 --- a/src/stores/stateBin.ts +++ b/src/stores/stateBin.ts @@ -1,17 +1,22 @@ import { browser } from '$app/environment'; import { writable, get, type Writable } from 'svelte/store'; +import localforage from 'localforage'; type StateBin = Record<string, unknown>; const STORAGE_KEY = 'stateBin'; -const initialState = browser ? JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}') : {}; -const baseStore = writable<StateBin>(initialState); +const baseStore = writable<StateBin>({}); -if (browser) - baseStore.subscribe((val) => { - localStorage.setItem(STORAGE_KEY, JSON.stringify(val)); +if (browser) { + localforage.getItem<StateBin>(STORAGE_KEY).then((value) => { + if (value && typeof value === 'object') baseStore.set(value); }); + baseStore.subscribe((value) => { + localforage.setItem(STORAGE_KEY, value); + }); +} + const createProxyStore = (store: Writable<StateBin>) => { return new Proxy(store, { get(target, prop: string) { |