Skip to content
Closed
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/old-pens-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: maintain reactivity for properties of deriveds with no deps
10 changes: 9 additions & 1 deletion packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ export function update_derived(derived) {
// the underlying value will be updated when the fork is committed.
// otherwise, the next time we get here after a 'real world' state
// change, `derived.equals` may incorrectly return `true`
if (!current_batch?.is_fork) {
//
// deriveds with no deps should always update `derived.v`
// since they will never change and need the value after fork commits
if (!current_batch?.is_fork || derived.deps === null) {
derived.v = value;
}

Expand All @@ -381,6 +384,11 @@ export function update_derived(derived) {
if (effect_tracking() || current_batch?.is_fork) {
batch_values.set(derived, value);
}
// For deriveds with no deps, set CLEAN to prevent re-evaluation
// since they can never become dirty from dependency changes
if (derived.deps === null) {
set_signal_status(derived, CLEAN);
}
} else {
var status = (derived.f & CONNECTED) === 0 ? MAYBE_DIRTY : CLEAN;
set_signal_status(derived, status);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
class Y {
foo = $state(0);
}
let y = $derived(new Y());
</script>

<button onclick={() => y.foo++}>click</button>
<p>{y.foo}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
skip_no_async: true,
async test({ assert, target }) {
const forkButton = target.querySelector('button');

flushSync(() => {
forkButton?.click();
});

const [, clickButton] = target.querySelectorAll('button');
const p = target.querySelector('p');

assert.equal(p?.textContent, '0');

flushSync(() => {
clickButton?.click();
});

assert.equal(p?.textContent, '1');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
import { fork } from 'svelte';
import Child from './Child.svelte';
let x = $state(false);
</script>

<button onclick={() => {
const f = fork(() => {
x = true;
});
f.commit();
}}>fork</button>

{#if x}
<Child />
{/if}
Loading