-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: adds $state.link rune #12545
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
feat: adds $state.link rune #12545
Conversation
🦋 Changeset detectedLatest commit: 3cd37ac The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we want to proxify the incoming value? If you pass in a raw value it would get proxified even if you don't want that.
On the other hand, if someone would want to mutate the linked value, but not mutate the original value, they would use something like $state.snapshot
, but if the result isn't proxified you won't have a way of mutating it - mhm..
Almost feels like $state.link
should get passed an arrow function so that you can control that:
// deep copy, mutations are reactive
const link1 = $state.link(() => { const copy = $state($state.snapshot(original)); return copy; });
// deep copy, mutations are not reactive
const link2 = $state.link(() => $state.snapshot(original));
// shallow copy, mutations maybe reactive but go through to original
const link3 = $state.link(() => original);
Alternatively there's a second argument that controls that, or there's $state.link
and $state.raw_link
Co-authored-by: Simon H <[email protected]>
Co-authored-by: Simon H <[email protected]>
Yep, this should apply deep-state. The intention is to add a |
Didn't want to leave this unaddressed. I took a closer look at Screen.Recording.2024-08-20.at.12.18.14.PM.movNotice that, since optimistic updates are 'replayed' on top of the new state, messages can appear to be both sent and sending. I'm left wondering if I've just misunderstood something, because that doesn't seem like a particularly useful API. Either way, it allays my concern that we missed something. Screen.Recording.2024-08-20.at.12.41.52.PM.mov |
I found a few issues with this hook not working well with multiple concurrent operations, as shown in your video. |
Yay! Hehe. Thanks, fine gents. Don't you DARE take it away from me now!! 😄 |
This reverts commit 63ec2e2.
Don't hate me, but I am thinking about it. The implementation in this PR didn't quite work, and the fixed version is simple enough that I can't really think of a good reason this shouldn't just exist in userland: svelte/packages/svelte/src/internal/client/reactivity/sources.js Lines 56 to 75 in 1534c1d
|
Damn! LOL. I won't say a thing. I already exposed my issue and how un-sveltey the workaround felt. In the end, you're the gatekeeper between us mortals and the almighty Svelte. I just want to love Svelte v5 as much and even more than v4. Version 4 made me enjoy UI development for the first time ever, I think. I've been back-end for 20 years. |
This reverts commit 63ec2e2.
This PR adds the
$state.link
rune, which acts like$state
in that you can write to the value and if it's an object or array, it will be deeply reactive. However, the difference is that unlike$state
where the passed in value is the initial value,$state.link
will keep up to day with consistent one-way link from the passed in value. Mutating the linked state locally will only mutate the value locally – it will not propagate the state change to the value that it was linked to.This PR still needs docs, so marking as WIP for now.
closes #12364