trifid/tfweb/src/lib/PersistentStore.ts

11 lines
405 B
TypeScript

import type { Writable } from "svelte/store";
import {writable} from "svelte/store";
import {browser} from "$app/environment";
export function persist(name: string, def_val = ""): Writable<any> {
const store = writable(browser && localStorage.getItem(name) || def_val);
store.subscribe((value: any) => {
if (browser) return (localStorage.setItem(name, value));
});
return store;
}