Skip to content
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

fix: only make $state variables reactive when read #15529

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ocean-OS
Copy link
Contributor

Currently, if a variable is wrapped in $state, it will only actually be wrapped in $.state if it is reassigned. However, this doesn't account for whether or not the variable is read. This PR fixes this, so that only $state variables that are written to and read from are perceived as reactive. Here's an example:

let count = $state(0);
function reset() {
    count = 0;
}

This code declares a reactive variable and assigns to it, but never reads it, meaning that it wouldn't have to be reactive. Meanwhile this...

let count = $state(0);
function increment() {
    count++;
}

Is reactive, since to increment count, its previous value has to be accessed.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Mar 16, 2025

🦋 Changeset detected

Latest commit: 766e6f0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

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

@svelte-docs-bot
Copy link

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15529

@Rich-Harris
Copy link
Member

Are there any realistic scenarios were this optimisation would apply, but that editor tooling wouldn't report the variable as unused? I think it's preferable to avoid adding complexity to the compiler unless there's a tangible benefit

@Ocean-OS
Copy link
Contributor Author

Are there any realistic scenarios were this optimisation would apply, but that editor tooling wouldn't report the variable as unused? I think it's preferable to avoid adding complexity to the compiler unless there's a tangible benefit

Well, one of the main reasons I made this was because I noticed in my bundles I would see lone $.states and $.deriveds because the bundler couldn't tell if the function was pure or not, and I think that it'd also add a little bit of a performance boost.

@Rich-Harris
Copy link
Member

Can you share a repro? Am a little surprised to hear that, but I might be misunderstanding

@trueadm
Copy link
Contributor

trueadm commented Mar 17, 2025

Well, one of the main reasons I made this was because I noticed in my bundles I would see lone $.states and $.deriveds because the bundler couldn't tell if the function was pure or not, and I think that it'd also add a little bit of a performance boost.

The fix sounds like we need to add /*#__NO_SIDE_EFFECTS__*/ around $.state and $.derived then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants