-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Expose a $state contract for use outside of .svelte.js and .svelte files #15011
Comments
One thing you could do is have an inner class BasicReactiveMap {
#hook = $state(0);
#internal = new Map;
get(key) {
this.#hook; //access to "hook into" Svelte's reactivity
return this.#internal.get(key);
}
set(key, value) {
if(this.#internal.get(key) !== value) {
this.#internal.set(key, value);
this.#hook++; //mutate to trigger any effects or deriveds
}
}
} I do understand that that isn't exactly what you asked for, but it could help, for example, if you were to export a similar hook function like this from a function hook() {
let x = $state(0);
return [()=>x, ()=>++x]; //returns tuple with `get` and `set` hooks
} |
See #12956. But, I don't really see how that or the contract will fix the nested reactivity problem without using runes and |
I don't quite understand what you're asking, do you want states declared and modified outside of svelte (.svelte and .svelte.js files) to have fine-grained reactivity when read inside svelte? I don't think this is possible, the best option I can think of is to add a method or getter to your observables that uses createSubscriber, but these methods won't be able to be abstracted away. <p>{foo.$.bar.$.baz.$}</p> // .$ is a getter that uses the subscriber returned by createSubscriber |
I'm not looking for fine-grained reactivity inside my observables - if a svelte component uses But I am looking for a way to do something like this:
Such that if the value of the I think However, this does imply that On reflection this may not be possible to do without a svelte dependency because you essentially need to register the dependency as a side-effect of reading it (which is how observables work already). |
Describe the problem
We have a fairly substantial application framework that heavily uses observables (as-in knockout style). Svelte's stores are an amazing fit - we made our observables implement the store contract and mostly this works great - with one exception: the fact that the syntax doesn't allow for nested stores, e.g. you can't do
foo.$bar
.When svelte 5 was announced with
$state
and using proxies/getters I was sure this would allow us to fix this problem and everything would be perfect. Obviously$state
itself is a compile-time transform, but I'm really surprised to find that there doesn't seem to be any kind of "contract" I can implement in a non-svelte object to make it observable to svelte as if I had used$state
.Describe the proposed solution
A documented "contract" (similar to the store contract) for making arbitrary objects reactive to svelte, without having to use
$state
and.svelte.js
files, or import svelte internals.Importance
would make my life easier
The text was updated successfully, but these errors were encountered: