diff --git a/app/src/lib/utils.ts b/app/src/lib/utils.ts index e845cc7..e979218 100644 --- a/app/src/lib/utils.ts +++ b/app/src/lib/utils.ts @@ -1,3 +1,5 @@ +import { writable } from 'svelte/store'; + export const humanFileSize = (size:number):string => { var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024)); return Number((size / Math.pow(1024, i)).toFixed(2)) * 1 + ['B', 'kB', 'MB', 'GB', 'TB'][i]; @@ -5,4 +7,16 @@ export const humanFileSize = (size:number):string => { export const lerp = (start: number, end: number, amt: number) => { return (1 - amt) * start + amt * end; -}; \ No newline at end of file +}; + +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; +} \ No newline at end of file