Replies: 7 comments 21 replies
-
Which part doesn't feel right: The persisted part, or the store part? I guess the store part? |
Beta Was this translation helpful? Give feedback.
-
It appears Stores are not longer necessary in Svelte. From https://svelte.dev/blog/runes
I think we should take everything in this repo and start a new project called I will create a new project that offers all the same options. |
Beta Was this translation helpful? Give feedback.
-
This is the basic idea of how the rune would work: export function persisted(key, initial) {
if (typeof(window) == 'undefined') {
throw Error('Cannot use server side')
}
const existing = localStorage.getItem(key)
let state = $state(existing ? JSON.parse(existing) : initial)
$effect(() => {
localStorage.setItem(key, JSON.stringify(state))
})
return {
get value() {
return state
},
set value(new_state) {
state = new_state
},
reset() {
state = initial
}
}
} |
Beta Was this translation helpful? Give feedback.
-
https://twitter.com/puruvjdev/status/1787037268143689894/photo/1 Comment with references, going to try this out and see how it works. |
Beta Was this translation helpful? Give feedback.
-
I have not looked over this thread fully, so I apologize if I am not contributing here, but I just wanted to throw this example out here to see if it helps at all import { browser } from '$app/environment';
export class LocalStore<T> {
value = $state<T>() as T;
key = '';
constructor(key: string, value: T) {
this.key = key;
this.value = value;
if (browser) {
const item = localStorage.getItem(key);
if (item) this.value = this.deserialize(item);
}
$effect(() => {
localStorage.setItem(this.key, this.serialize(this.value));
});
}
serialize(value: T): string {
return JSON.stringify(value);
}
deserialize(item: string): T {
return JSON.parse(item);
}
}
export function localStore<T>(key: string, value: T) {
return new LocalStore(key, value);
} via Joy of Code I have not tried this yet so I don't know if there are pitfalls. (classic Stack Overflow response, sry!) |
Beta Was this translation helpful? Give feedback.
-
in the end a new package has been created or are you going to be updating this one? |
Beta Was this translation helpful? Give feedback.
-
Here is my current implementation which is not perfect. https://github.com/exceptionless/Exceptionless/blob/main/src/Exceptionless.Web/ClientApp/src/lib/features/shared/persisted.svelte.ts |
Beta Was this translation helpful? Give feedback.
-
I've been upgrading my app to svelte 5 and one thing that no longer feels idiomatic is using persisted stores in a runes world. This needs a discussion into what is the best option.
Beta Was this translation helpful? Give feedback.
All reactions