Skip to content

Commit fa5d3a9

Browse files
authored
fix: ensure inspect effects are skipped from effect parent logic (#12810)
1 parent 34ad016 commit fa5d3a9

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

.changeset/stupid-cars-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure inspect effects are skipped from effect parent logic

packages/svelte/src/internal/client/reactivity/effects.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export function push_effect(effect, parent_effect) {
8181
*/
8282
function create_effect(type, fn, sync, push = true) {
8383
var is_root = (type & ROOT_EFFECT) !== 0;
84+
var parent_effect = current_effect;
85+
86+
if (DEV) {
87+
// Ensure the parent is never an inspect effect
88+
while (parent_effect !== null && (parent_effect.f & INSPECT_EFFECT) !== 0) {
89+
parent_effect = parent_effect.parent;
90+
}
91+
}
8492

8593
/** @type {Effect} */
8694
var effect = {
@@ -92,7 +100,7 @@ function create_effect(type, fn, sync, push = true) {
92100
fn,
93101
last: null,
94102
next: null,
95-
parent: is_root ? null : current_effect,
103+
parent: is_root ? null : parent_effect,
96104
prev: null,
97105
teardown: null,
98106
transitions: null,
@@ -130,8 +138,8 @@ function create_effect(type, fn, sync, push = true) {
130138
effect.teardown === null;
131139

132140
if (!inert && !is_root && push) {
133-
if (current_effect !== null) {
134-
push_effect(effect, current_effect);
141+
if (parent_effect !== null) {
142+
push_effect(effect, parent_effect);
135143
}
136144

137145
// if we're in a derived, add the effect there too
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
dev: true
6+
},
7+
8+
async test({ assert, logs }) {
9+
assert.deepEqual(logs, ['init', 0, 'update', 1]);
10+
}
11+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
let a = $state(0);
3+
let b = $derived.by(() => {
4+
$effect(() => {
5+
a = 1;
6+
})
7+
return a;
8+
})
9+
10+
$inspect(b);
11+
</script>
12+

0 commit comments

Comments
 (0)