Open kkolinko opened 2 weeks ago
I debugged this a bit, and it seems that the $.proxy(value)
call inside set items(value)
comes too late. It seems the getter returns a value that is not proxified yet? It's very strange, and I'm wondering if this is a browser bug or spec gotcha or something else.
Describe the problem
Nullish coalescing assignment (??=) operator may be used to update fields that are either null or a non-empty array.
Such code pattern can be seen in Svelte internals, e.g. (internal/client/runtime.js):
(dependency.reactions ??= []).push(reaction);
If I try to use the same pattern, the results differ on whether I deal with a class field vs with a variable in a component.
Essentially, the code is the following:
The result is displayed with explicit conversion to string ("" + ...) so that I can differ a "null" value from an empty array.
For a class the code is
(Explicitly converting to a string ).
Expected behaviour: I expect to see the following, after each click on an "Add" button:
Actual behaviour
"addItem3", "addItem4" work as expected, correctly.
"addItem2" works correctly for a Class, but it does not work for a Component: I see "null", then "1", "1", "1" (the value is not updated).
"addItem" does not work for a Class and does not work for a Component For a Class I see: "null", "", "2", "2,3", "2,3,4", ... (the "1" is lost) For a Component I see "null", then "1", "1", "1" (the value is not updated).
Describe the proposed solution
I think that it can be left as is and documented as a limitation of the $state rune.
Though if I look at the code, generated by Svelte 5.1.15, for the case of a Component it is:
I see that in addItem() and addItem2() the value could be wrapped with
$.proxy(...)
like it was done for addItem3():In the case of a class the code is:
The addItem() could be:
based on the fact that second invocation of proxy() inside the setter method will be effectively a noop. I have not come up with a good solution for this case yet.
Importance
would make my life easier