Skip to content

fix: improve unowned derived signal heuristics #11029

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 2 commits into from
Apr 2, 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/big-moons-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve unowned derived signal heuristics
7 changes: 5 additions & 2 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export function check_dirtiness(reaction) {

if ((flags & MAYBE_DIRTY) !== 0) {
var dependencies = reaction.deps;
var is_unowned = (flags & UNOWNED) !== 0;

if (dependencies !== null) {
var length = dependencies.length;
Expand All @@ -191,7 +192,6 @@ export function check_dirtiness(reaction) {
// if our dependency write version is higher. If it is then we can assume
// that state has changed to a newer version and thus this unowned signal
// is also dirty.
var is_unowned = (flags & UNOWNED) !== 0;
var version = dependency.version;

if (is_unowned && version > /** @type {import('#client').Derived} */ (reaction).version) {
Expand All @@ -201,7 +201,10 @@ export function check_dirtiness(reaction) {
}
}

set_signal_status(reaction, CLEAN);
// Unowned signals are always maybe dirty, as we instead check their dependency versions.
if (!is_unowned) {
set_signal_status(reaction, CLEAN);
}
}

return false;
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 }) {
let [btn1] = target.querySelectorAll('button');

flushSync(() => {
btn1.click();
});

assert.htmlEqual(
target.innerHTML,
`<button>update</button><div>2</div><div>2</div><div>2</div><div>2</div>`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
let thing = $state();

function update() {
let data = $state({ name: 1, position: 1 });
let position = $derived(data.position);
let name = $derived(data.name);

thing = {
get data() { return data },
get position() { return position },
get name() { return name },
}

thing.position;
data = { name: 2, position: 2 };
}
</script>

<button onclick={update}>update</button>

<div>
{thing?.data?.name}
</div>
<div>
{thing?.name}
</div>
<div>
{thing?.data?.position}
</div>
<div>
{thing?.position}
</div>