From fa5d3a9002106c9abc15f25dfafef1e24ca4ae7a Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 12 Aug 2024 16:26:32 +0100 Subject: [PATCH] fix: ensure inspect effects are skipped from effect parent logic (#12810) --- .changeset/stupid-cars-behave.md | 5 +++++ .../src/internal/client/reactivity/effects.js | 14 +++++++++++--- .../samples/inspect-nested-effect/_config.js | 11 +++++++++++ .../samples/inspect-nested-effect/main.svelte | 12 ++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 .changeset/stupid-cars-behave.md create mode 100644 packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/main.svelte diff --git a/.changeset/stupid-cars-behave.md b/.changeset/stupid-cars-behave.md new file mode 100644 index 0000000000..9612cb3a86 --- /dev/null +++ b/.changeset/stupid-cars-behave.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure inspect effects are skipped from effect parent logic diff --git a/packages/svelte/src/internal/client/reactivity/effects.js b/packages/svelte/src/internal/client/reactivity/effects.js index 9fd238c866..d05243bb14 100644 --- a/packages/svelte/src/internal/client/reactivity/effects.js +++ b/packages/svelte/src/internal/client/reactivity/effects.js @@ -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 = { @@ -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, @@ -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 diff --git a/packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/_config.js b/packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/_config.js new file mode 100644 index 0000000000..82429e5e36 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/_config.js @@ -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]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/main.svelte b/packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/main.svelte new file mode 100644 index 0000000000..3693e4f6d5 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/inspect-nested-effect/main.svelte @@ -0,0 +1,12 @@ + +