-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add rune to more easily combine reactive values and state objects #11739
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
I'm aware that state and derived can be used on class fields. I suppose you could extend classes to accomplish something similar, but that doesn't permit for the kind of composability that I am looking for since you can't merge two classes, again, as far as I know. |
If I understood you correctly something similar is already discussed in #11210. |
Similar, yes. But not the same. I don't care much about writing getters and setters the one time if that's an issue, but it becomes a pain when I have to do it to combine state objects. It also makes the The recommended solution here is also slightly different, as it is not used on object properties or array elements but takes in state and flattens it into an object. However, whether that is different depends on how the compiler works. |
You can use |
Describe the problem
Problem
I'm finding a lot of use out of separating portions of state into different objects and combining them as needed. For example, I am working on a project for playing chess with variants. Every single variant is going to have some number of state properties, but also some state specific to that variant.
In order for derived values and state values to exist on the same object, we have to return an object with getters and setters (AFAIK).
So, the base state can be created as follows:
and then for something like standard chess, you will also have some more specific state:
Now, if I want all this state on the same object, I'm going to have to something like this:
This is clearly a lot of code for the task. I'm not an expert, but I wonder if there is also a performance hit associated with this kind of stuff that might not be a problem if handled at compile time.
Why would you want to do this?
I find there is a benefit in that I can create specific types for different pieces of state without constructing complex nested state objects. Additionally, having derived and state values on the same object is desirable in many cases.
Describe the proposed solution
It's hard to say for sure, as I don't know exactly how the internals of svelte 5 work, but something like a rune that makes it easier to combine state.
$restate
might be confusing, so there could be$merge
,$compose
,$combine
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: