-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5 Exporting $state compile problem #14316
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
Comments
This is somewhat on purpose - we want to restrict the places where you can use runes to not make it look like it's something that it isn't. We're open to loosen that in the future if more use cases come up / we're confident that the rules of runes are well understood enough. |
Can this be made to work, @dummdidumm ? It is my understanding that $state is more an L-value than what it looks like (R-value). |
What we could do is permit |
I also found certain restrictions are overly strict, e.g. Svelte compiler doesn't allow this for existing objects: const obj = {};
obj.count = $state(0); So, the workaround would have to be this but it's pretty cumbersome: const obj = {};
let count = $state(0);
Object.defineProperty(obj, 'count', {
get: function() { return count },
set: function(v) { count = v},
enumerable: true,
}); This works though since obj becomes a proxy but the whole object already has to be a state: const obj = $state({});
obj.count = 0; Incidentally, using
if const obj = $state({});
let count = $state(0);
Object.defineProperty(obj, 'count', {
get: function() { return count },
set: function(v) { count = v},
enumerable: true,
configurable: true,
}); |
Just make the original object state, then you don’t need to create state to assign to it. |
right, i have it outlined in my comment above. just saying that if there is a non-state object, it's cumbersome to add properties to it vs just a simple assignment. And, for a state object defining custom getters and setters for a property doesn't work. |
You're trying to add a reactive state of
Please can you create a separate issue for that, seems like a bug. |
Just for clarity, what I was aiming for is for svelte to perform a code transformation when assigning state directly. assigning state with primitives: obj.count = $state(0); svelte compiles into something like this: let count_1 = $.state(42);
Object.defineProperties(obj, {
count: {
get() {
return $.get(count_1);
},
set(v) {
$.set(count_1, $.proxy(v, null, count_1));
},
enumerable: true
}
}); and for assigning state with objects: obj.counter = $state({count: 42}) svelte compiles into something like this: let counter_1 = $.state($.proxy({ count: 42 }));
Object.defineProperties(obj, {
counter: {
get() {
return $.get(counter_1);
},
set(v) {
$.set(testCount, $.proxy(v, null, counter_1));
},
enumerable: true
}
});
👍 done! #14320 |
That's far too magical and simply assumes too much. If Oh and |
@dummdidumm @trueadm - On the original issue, is it possible to get the return value to work? Thanks! J |
FWIW, The |
Direct object property is almost double the test for me |
It appears you've not setup the Proxy properly in your benchmark, you're only handling |
I was just testing pass throughs without traps. But yeah, if we add a trap for with defineProperty, get, set traps: |
Our internal proxy implementation has it, as we don't allow modifications to the underlying target object. |
@trueadm - Any note on my original post? Could we still permit Thanks! J |
Describe the bug
If I want to export a shared state, the normal way is to do like this:
Option 1
Option 2
This also works:
Option 3
However, if I simplify option 2, I get a compile error:
I get the error:
I would expect Option 3 to compile the exact same way as option 2 !?
Reproduction
REPL
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: