Skip to content

fix: always mark reactions of deriveds #16457

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 4 commits into from
Jul 20, 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/angry-hornets-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: always mark reactions of deriveds
4 changes: 3 additions & 1 deletion packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ export function update_derived(derived) {

// don't mark derived clean if we're reading it inside a
// cleanup function, or it will cache a stale value
if (is_destroying_effect) return;
if (is_destroying_effect) {
return;
}

if (batch_deriveds !== null) {
batch_deriveds.set(derived, derived.v);
Expand Down
19 changes: 8 additions & 11 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,6 @@ function mark_reactions(signal, status) {
var reaction = reactions[i];
var flags = reaction.f;

// Skip any effects that are already dirty
if ((flags & DIRTY) !== 0) continue;

// In legacy mode, skip the current effect to prevent infinite loops
if (!runes && reaction === active_effect) continue;

Expand All @@ -326,15 +323,15 @@ function mark_reactions(signal, status) {
continue;
}

set_signal_status(reaction, status);
// don't set a DIRTY reaction to MAYBE_DIRTY
if ((flags & DIRTY) === 0) {
set_signal_status(reaction, status);
}

// If the signal a) was previously clean or b) is an unowned derived, then mark it
if ((flags & (CLEAN | UNOWNED)) !== 0) {
if ((flags & DERIVED) !== 0) {
mark_reactions(/** @type {Derived} */ (reaction), MAYBE_DIRTY);
} else {
schedule_effect(/** @type {Effect} */ (reaction));
}
if ((flags & DERIVED) !== 0) {
mark_reactions(/** @type {Derived} */ (reaction), MAYBE_DIRTY);
} else if ((flags & DIRTY) === 0) {
schedule_effect(/** @type {Effect} */ (reaction));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { tick } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
await tick();

const [a, b, update] = target.querySelectorAll('button');

assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<button>0</button>
<h1>a</h1>
`
);

b.click();
await tick();
assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<button>0</button>
<h1>b</h1>
`
);

update.click();
await tick();
assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<button>1</button>
<h1>b</h1>
`
);

a.click();
await tick();
assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<button>1</button>
<h1>a</h1>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script>
let object = $state(null);
let count = $state(0);
const condition = $derived(object === null);
</script>

<svelte:boundary>
<button onclick={() => (object = null)}>a</button>
<button onclick={() => (object = {})}>b</button>

<button onclick={async () => {
count++;
await Promise.resolve();
object = {};
}}>{await count}</button>

{#if condition}
<h1>a</h1>
{:else}
<h1>b</h1>
{/if}

{#snippet pending()}{/snippet}
</svelte:boundary>

Loading