trifid/tfweb/src/lib/PersistentStore.ts

11 lines
405 B
TypeScript
Raw Normal View History

2023-02-20 04:23:19 +00:00
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;
}