New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svelte 5: Add a way to mark objects as stale #11423
Comments
Isn't this what |
|
You can nest the data in a simple wrapper, then create a new wrapper in the assignment. let obj = $state({ data: {} });
setInterval(() => obj = { ...obj }, 1000) |
Try to use |
What are your real cases? Why do you want to do something if the data didn't change? |
You can use a class to handle external state and trigger class External {
#data;
#version = $state(0);
constructor(data) {
this.#data = data;
}
get data() {
this.#version;
return this.#data;
}
set data(_data) {
this.#version++;
this.#data = _data;
}
invalidate() {
this.#version++;
}
} |
A quick and dirty fix to get the v4 behaviour (without object cloning) is to set the variable to undefined, and then reset it back to the previous state: <script lang="ts">
let obj = $state({});
setInterval(()=> {
let tmp = obj;
obj = undefined;
obj = tmp;
}, 1000)
$effect(()=>{
obj;
console.log("An update happened")
})
</script> |
I just had a look at the reactive collections provided by Svelte (in I'm glad to find some workarounds, but it would be nice to have a more idiomatic solution down the line. I'm willing to contribute a PR if the members can agree on a solution. |
This isn't something we feel needs to be a rune or in the core of Svelte, given how you can address this in various different ways in userland. So closing this for now. |
Describe the problem
In previous versions of Svelte, reassigning objects to a store or variable will trigger an update.
For example, this will log to the console every second.
In Svelte 5, states do not cause an update during object reassignment, due to the new fine grained reactivity model.
This will only log once during initialization.
This is an issue, I often use re-assignment to mark data from external APIs as stale.
This has prevented me from replacing some stores with state-runes, as the only workaround is to clone the object, adding overhead.
Describe the proposed solution
Add a rune to mark some data as stale, forcing it and all of its dependencies to update.
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: