🍭 Updates the persistent store to save onload

This commit is contained in:
Rune Harlyk
2024-08-24 21:04:30 +02:00
committed by Rune Harlyk
parent ce1558a6ab
commit 16e653afa8
+5 -3
View File
@@ -4,11 +4,13 @@ import { browser } from '$app/environment';
export const persistentStore = <T>(key: string, initialValue: T) => {
const savedValue = browser ? localStorage.getItem(key) : null;
const data: T = savedValue !== null ? JSON.parse(savedValue) : initialValue;
const store = writable<T>(data);
const store = writable<T>();
store.subscribe((value) => {
store.subscribe(value => {
if (browser) localStorage.setItem(key, JSON.stringify(value));
});
store.set(data);
return store;
};
};