-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Description
Describe the bug
When nesting two $effect.pres, where the outer one depends on a value the inner one mutates, the outer effect isn't re-run. This is inconsistent with the simpler case where mutation happens directly in the outer effect, or when using $effect instead of $effect.pre.
Reproduction
The following code demonstrates the issue:
let value = $state(0);
$effect.pre(() => {
console.log("Outer");
value; // outer effect is made dependent on value
$effect.pre(() => {
console.log("Inner");
value = 10; // inner effect mutates value
});
});
// Expected output: Outer, Inner, Outer, Inner
// Actual output: Outer, InnerExpected behavior
Accessing the value in the outer effect establishes a dependency. After mutating the value in the inner effect, the outer effect should re-run. (And hence, a new inner effect should be created). The expected output is: Outer, Inner, Outer, Inner.
Actual behavior
The outer effect is not re-run after the value changes. Hence we get the output Outer, Inner.
Comparison case 1
When mutating the value directly in the outer effect, it is correctly re-run:
let value = $state(0);
$effect.pre(() => {
console.log("Outer");
value; // outer effect is made dependent on value
value = 10; // outer effect mutates value
});
// Expected output: Outer, Outer
// Actual output: Outer, OuterComparison case 2
When replacing the inner $effect.pre with $effect, the outer effect is correctly re-run:
let value = $state(0);
$effect.pre(() => {
console.log("Outer");
value; // outer effect is made dependent on value
$effect(() => {
console.log("Inner");
value = 10; // inner effect mutates value
});
});
// Expected output: Outer, Inner, Outer, Inner
// Actual output: Outer, Inner, Outer, InnerReproduction
https://svelte.dev/playground/4fb0fdc4fad240b99a06501f6c80df24?version=5.23.0
Logs
System Info
System:
OS: macOS 15.3.1
CPU: (10) arm64 Apple M1 Max
Memory: 15.04 GB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.9.0 - /opt/homebrew/bin/node
npm: 11.2.0 - /opt/homebrew/bin/npm
Browsers:
Chrome: 134.0.6998.88
Safari: 18.3
npmPackages:
svelte: ^5.15.0 => 5.23.0Severity
annoyance