🪖 Moves persistent store to svelte-utilities
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from './result'
|
||||
export * from './string-utilities'
|
||||
export * from './svelte-utilities'
|
||||
export * from './math-utilities'
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
export const persistentStore = (key:string, initialValue:any) => {
|
||||
const savedValue = JSON.parse(localStorage.getItem(key) as string);
|
||||
const data = savedValue !== null ? savedValue : initialValue;
|
||||
const store = writable(data);
|
||||
|
||||
store.subscribe(value => {
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
});
|
||||
|
||||
return store;
|
||||
}
|
||||
Reference in New Issue
Block a user