allow writing to public deriveds on server

pull/15964/head
Simon Holthausen 4 months ago
parent 846d780415
commit 5ba35727d2

@ -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')))])
);
}
}
@ -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(
@ -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')))])
);
}
}

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
html: `3 3 3 3`
});

@ -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>
{x.on_class} {x.in_constructor} {x.on_class_private} {x.in_constructor_private}
Loading…
Cancel
Save