Skip to content

Conversation

@Ocean-OS
Copy link
Member

Fixes #15621 by adding a setter to $derived class fields.

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

@changeset-bot
Copy link

changeset-bot bot commented Mar 27, 2025

🦋 Changeset detected

Latest commit: 03d035a

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

@github-actions
Copy link
Contributor

Playground

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

@dummdidumm
Copy link
Member

Looks good - add a test and we can merge this 👍

@Ocean-OS
Copy link
Member Author

Just realized I haven't implemented this on the server, how should I go about doing that? I'm not sure if we should allow setting it, or if we should have a helper function for it. I'm thinking more towards a helper function, that'd look like this:

function server_derived(fn) {
    let value;
    let computed_wv = 1;
    let write_wv = 1;
    let last_computed_value;
    let wv = 1;
    function read() {
        if (computed_wv < write_wv) {
            let val = fn();
            if (val !== last_computed_value) {
                value = val;
                computed_wv = ++wv;
                last_computed_value = val;
            }
        }
        if (write_wv === 1) {
            if (computed_wv === 1) {
                value = fn();
                computed_wv = ++wv;
                last_computed_value = value;
            } else {
                let val = fn();
                if (val !== last_computed_value) {
                    value = val;
                    computed_wv = ++wv;
                    last_computed_value = val;
                }
            }
        }
        return value;
    }
    function write(next) {
        write_wv = ++wv;
        value = next;
    }
    return [read, write];
}

@dummdidumm
Copy link
Member

@paoloricciuti started something related in #14977, maybe it's best to not worry about writable derived class fields for now (if you eagerly update them during server rendering, then wtf are you even doing) and incorporate that into #14977 instead.

@Rich-Harris Rich-Harris merged commit ec91cac into sveltejs:main Mar 31, 2025
9 checks passed
@github-actions github-actions bot mentioned this pull request Mar 31, 2025
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.

Add setter to $derived property

3 participants