Skip to content

fix: ensure inspect effects are skipped from effect parent logic #12810

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 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/stupid-cars-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure inspect effects are skipped from effect parent logic
14 changes: 11 additions & 3 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export function push_effect(effect, parent_effect) {
*/
function create_effect(type, fn, sync, push = true) {
var is_root = (type & ROOT_EFFECT) !== 0;
var parent_effect = current_effect;

if (DEV) {
// Ensure the parent is never an inspect effect
while (parent_effect !== null && (parent_effect.f & INSPECT_EFFECT) !== 0) {
parent_effect = parent_effect.parent;
}
}

/** @type {Effect} */
var effect = {
Expand All @@ -92,7 +100,7 @@ function create_effect(type, fn, sync, push = true) {
fn,
last: null,
next: null,
parent: is_root ? null : current_effect,
parent: is_root ? null : parent_effect,
prev: null,
teardown: null,
transitions: null,
Expand Down Expand Up @@ -130,8 +138,8 @@ function create_effect(type, fn, sync, push = true) {
effect.teardown === null;

if (!inert && !is_root && push) {
if (current_effect !== null) {
push_effect(effect, current_effect);
if (parent_effect !== null) {
push_effect(effect, parent_effect);
}

// if we're in a derived, add the effect there too
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},

async test({ assert, logs }) {
assert.deepEqual(logs, ['init', 0, 'update', 1]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let a = $state(0);
let b = $derived.by(() => {
$effect(() => {
a = 1;
})
return a;
})

$inspect(b);
</script>

Loading