Skip to content

fix: run effect roots in tree order #15446

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 5 commits into from
Mar 5, 2025
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/shy-falcons-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: run effect roots in tree order
24 changes: 15 additions & 9 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ function push_effect(effect, parent_effect) {
* @returns {Effect}
*/
function create_effect(type, fn, sync, push = true) {
var is_root = (type & ROOT_EFFECT) !== 0;
var parent_effect = active_effect;
var parent = active_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;
while (parent !== null && (parent.f & INSPECT_EFFECT) !== 0) {
parent = parent.parent;
}
}

Expand All @@ -103,7 +102,7 @@ function create_effect(type, fn, sync, push = true) {
fn,
last: null,
next: null,
parent: is_root ? null : parent_effect,
parent,
prev: null,
teardown: null,
transitions: null,
Expand Down Expand Up @@ -136,9 +135,9 @@ function create_effect(type, fn, sync, push = true) {
effect.teardown === null &&
(effect.f & (EFFECT_HAS_DERIVED | BOUNDARY_EFFECT)) === 0;

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

// if we're in a derived, add the effect there too
Expand Down Expand Up @@ -391,7 +390,14 @@ export function destroy_effect_children(signal, remove_dom = false) {

while (effect !== null) {
var next = effect.next;
destroy_effect(effect, remove_dom);

if ((effect.f & ROOT_EFFECT) !== 0) {
// this is now an independent root
effect.parent = null;
} else {
destroy_effect(effect, remove_dom);
}

effect = next;
}
}
Expand Down
14 changes: 5 additions & 9 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,7 @@ function flush_queued_root_effects() {
queued_root_effects = [];

for (var i = 0; i < length; i++) {
var root = root_effects[i];

if ((root.f & CLEAN) === 0) {
root.f ^= CLEAN;
}

var collected_effects = process_effects(root);
var collected_effects = process_effects(root_effects[i]);
flush_queued_effects(collected_effects);
}
}
Expand Down Expand Up @@ -759,11 +753,12 @@ function process_effects(root) {
/** @type {Effect[]} */
var effects = [];

var effect = root.first;
/** @type {Effect | null} */
var effect = root;

while (effect !== null) {
var flags = effect.f;
var is_branch = (flags & BRANCH_EFFECT) !== 0;
var is_branch = (flags & (BRANCH_EFFECT | ROOT_EFFECT)) !== 0;
var is_skippable_branch = is_branch && (flags & CLEAN) !== 0;

if (!is_skippable_branch && (flags & INERT) === 0) {
Expand All @@ -788,6 +783,7 @@ function process_effects(root) {
}
}

/** @type {Effect | null} */
var child = effect.first;

if (child !== null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target, logs }) {
const [b1, b2] = target.querySelectorAll('button');

flushSync(() => b1.click());
assert.deepEqual(logs, [0, 1]);

flushSync(() => b1.click());
assert.deepEqual(logs, [0, 1, 2]);

flushSync(() => b2.click());
assert.deepEqual(logs, [0, 1, 2]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
let obj = $state({ count: 0 });

$effect.root(() => {
let teardown;

$effect.pre(() => {
if (obj) {
teardown ??= $effect.root(() => {
$effect.pre(() => {
console.log(obj.count);
});
});
} else {
teardown?.();
teardown = null;
}
});
});
</script>

<button onclick={() => ((obj ??= { count: 0 }).count += 1)}>+1</button>
<button onclick={() => (obj = null)}>null</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
let [, btn2] = target.querySelectorAll('button');

btn2.click();
flushSync();

assert.htmlEqual(target.innerHTML, `<button>Set data</button><button>Clear data</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { toStore } from 'svelte/store'

let { data } = $props()
const currentValue = toStore(() => data.value)
</script>

<p>
Current value:
<span>{$currentValue}</span>
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import Child from './child.svelte'

let data = $state({ value: 'hello' });

const setData = () => (data = { value: 'hello' })
const clearData = () => (data = undefined)
</script>

<button onclick={setData}>Set data</button>
<button onclick={clearData}>Clear data</button>

{#if data}
<Child {data} />
{/if}