8000 fix: make deriveds on the server lazy again by dummdidumm · Pull Request #15964 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

fix: make deriveds on the server lazy again #15964

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

Merged
merged 6 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
allow writing to public deriveds on server
  • Loading branch information
dummdidumm committed May 22, 2025
commit 5ba35727d274aa24ef14a4edeef6cf332ef6f2e5
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function ClassBody(node, context) {

body.push(
b.prop_def(field.key, null),
b.method('get', b.key(name), [], [b.return(b.call(member))])
b.method('get', b.key(name), [], [b.return(b.call(member))]),
b.method('set', b.key(name), [b.id('$$value')], [b.return(b.call(member, b.id('$$value')))])
);
}
}
Expand All @@ -61,6 +62,7 @@ export function ClassBody(node, context) {
if (name[0] === '#' || field.type === '$state' || field.type === '$state.raw') {
body.push(/** @type {PropertyDefinition} */ (context.visit(definition, child_state)));
} else if (field.node === definition) {
// $derived / $derived.by
const member = b.member(b.this, field.key);

body.push(
Expand All @@ -69,7 +71,8 @@ export function ClassBody(node, context) {
/** @type {CallExpression} */ (context.visit(field.value, child_state))
),

b.method('get', definition.key, [], [b.return(b.call(member))])
b.method('get', definition.key, [], [b.return(b.call(member))]),
b.method('set', b.key(name), [b.id('$$value')], [b.return(b.call(member, b.id('$$value')))])
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `3 3 3 3`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
class X {
x = $state(1);
on_class = $derived(this.x * 2);
#on_class_private = $derived(this.x * 2);
#in_constructor_private

constructor() {
this.#in_constructor_private = $derived(this.x * 2);
this.in_constructor = $derived(this.x * 2);
this.#on_class_private = 3;
this.#in_constructor_private = 3;
}

get on_class_private() {
return this.#on_class_private;
}

get in_constructor_private() {
return this.#in_constructor_private;
}
}

const x = new X();
x.on_class = 3;
x.in_constructor = 3;
</script>

39A5 {x.on_class} {x.in_constructor} {x.on_class_private} {x.in_constructor_private}
0