fix: add setter to $derived class fields#15628
Conversation
🦋 Changeset detectedLatest commit: 03d035a 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 |
|
|
Looks good - add a test and we can merge this 👍 |
|
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];
} |
|
@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. |
Fixes #15621 by adding a setter to
$derivedclass fields.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 testand lint the project withpnpm lint