-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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: allow state created in deriveds/effects to be written/read locally without self-invalidation #15553
Conversation
…ts with $.derived
🦋 Changeset detectedLatest commit: 82c6f39 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 |
|
This comment was marked as resolved.
This comment was marked as resolved.
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.
Looks good. I wonder if there might be any unforeseen issues with keeping a round a reference to the parent
on state signals. Like if you create a proxied object in an effect with a ton of properties and then pass that to something else outside the effect. That effect will never be GC'd even if it's unmounted because all those state signals will retain that reference forever.
Good catch — addressed that by reinstating a version of the |
…elease should fix this anyway sveltejs/svelte#15553
This implements the idea in #15300 (comment). It means that state created inside a reaction (i.e. a derived or effect) can be written and read inside that reaction without causing it to re-run.
This means that things like this, which fail on
main
, because a simpleconsole.log(this.name)
causes an 'unsafe read'......work totally fine in this PR.
I don't see any real downside to this, and I don't see it as a breaking change (since the changed behaviour only concerns code that would have previously failed altogether). The only slight trickery concerns proxies — sources that are created lazily as properties are read need to have the same
parent
as the original$state
, otherwise in a situation like this, which should cause an infinite loop......no infinite loop would occur, because
object.count
would 'belong' to the effect. Userland does not have this ability, so anyone implementing their own lazy state mechanism would potentially find that assignments that 'should' cause an infinite loop don't. Personally I'm very okay with this.Draft because it could probably use a few tests; will try and get to that tomorrow.
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint