mirror of https://github.com/sveltejs/svelte
fix: ensure assignments to state field inside constructor trigger effect (#12985)
* fix: ensure assignments to state field inside constructor trigger effects * address feedback * address feedback * address feedback * add test * alternative approach * lint * error on reading local source in derived * build * add changeset for self-dependency error * revert unused changes * revert unused changes * Update packages/svelte/messages/client-errors/errors.md * regenerate * tweak --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/12998/head
parent
72b066b7fd
commit
975918c602
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
breaking: throw error if derived creates state and then depends on it
|
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: ensure assignments to state field inside constructor trigger effects
|
@ -0,0 +1,20 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<button>10</button>`,
|
||||
|
||||
test({ assert, target, logs }) {
|
||||
const btn = target.querySelector('button');
|
||||
|
||||
btn?.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(target.innerHTML, `<button>11</button>`);
|
||||
|
||||
btn?.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(target.innerHTML, `<button>12</button>`);
|
||||
|
||||
assert.deepEqual(logs, [0, 10, 11, 12]);
|
||||
}
|
||||
});
|
@ -0,0 +1,16 @@
|
||||
<script>
|
||||
class Counter {
|
||||
count = $state(0);
|
||||
constructor(initial) {
|
||||
$effect.pre(() => {
|
||||
console.log(this.count);
|
||||
});
|
||||
this.count = initial;
|
||||
}
|
||||
}
|
||||
const counter = $derived(new Counter(10));
|
||||
|
||||
counter;
|
||||
</script>
|
||||
|
||||
<button onclick={() => counter.count++}>{counter.count}</button>
|
@ -0,0 +1,20 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<button>10</button>`,
|
||||
|
||||
test({ assert, target, logs }) {
|
||||
const btn = target.querySelector('button');
|
||||
|
||||
btn?.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(target.innerHTML, `<button>11</button>`);
|
||||
|
||||
btn?.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(target.innerHTML, `<button>12</button>`);
|
||||
|
||||
assert.deepEqual(logs, [0, 10, 11, 12]);
|
||||
}
|
||||
});
|
@ -0,0 +1,14 @@
|
||||
<script>
|
||||
class Counter {
|
||||
count = $state(0);
|
||||
constructor(initial) {
|
||||
$effect.pre(() => {
|
||||
console.log(this.count);
|
||||
});
|
||||
this.count = initial;
|
||||
}
|
||||
}
|
||||
const counter = new Counter(10);
|
||||
</script>
|
||||
|
||||
<button onclick={() => counter.count++}>{counter.count}</button>
|
@ -0,0 +1,20 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<button>10</button>`,
|
||||
|
||||
test({ assert, target, logs }) {
|
||||
const btn = target.querySelector('button');
|
||||
|
||||
btn?.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(target.innerHTML, `<button>11</button>`);
|
||||
|
||||
btn?.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(target.innerHTML, `<button>12</button>`);
|
||||
|
||||
assert.deepEqual(logs, [0, 10, 11, 12]);
|
||||
}
|
||||
});
|
@ -0,0 +1,20 @@
|
||||
<script>
|
||||
class Base {
|
||||
count = $state(0);
|
||||
}
|
||||
|
||||
class Counter extends Base {
|
||||
constructor(initial) {
|
||||
super();
|
||||
$effect.pre(() => {
|
||||
console.log(this.count);
|
||||
});
|
||||
this.count = initial;
|
||||
}
|
||||
}
|
||||
const counter = $derived(new Counter(10));
|
||||
|
||||
counter;
|
||||
</script>
|
||||
|
||||
<button onclick={() => counter.count++}>{counter.count}</button>
|
Loading…
Reference in new issue