Created
October 5, 2022 08:25
-
-
Save zerodevx/6b74361d5a087a94a0104582bcedc820 to your computer and use it in GitHub Desktop.
Gist for simple persistent stores for SvelteKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { writable } from 'svelte/store' | |
import { browser } from '$app/environment' | |
function persist(name) { | |
const _store = browser && sessionStorage.getItem(name) | |
const store = writable(_store ? JSON.parse(_store) : '') | |
if (browser) { | |
store.subscribe((i) => sessionStorage.setItem(name, JSON.stringify(i))) | |
} | |
return store | |
} | |
export const myvar = persist('persisted_myvar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment