Skip to content
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

fix: ensure derived is detected as dirty correctly #11496

Merged
merged 1 commit into from
May 7, 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/shiny-melons-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: ensure derived is detected as dirty correctly
8 changes: 3 additions & 5 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ export function check_dirtiness(reaction) {

if (!is_dirty && check_dirtiness(/** @type {import('#client').Derived} */ (dependency))) {
update_derived(/** @type {import('#client').Derived} **/ (dependency), true);

// `signal` might now be dirty, as a result of calling `update_derived`
if ((reaction.f & DIRTY) !== 0) {
return true;
}
}

if (is_unowned) {
Expand All @@ -230,6 +225,9 @@ export function check_dirtiness(reaction) {
reactions.push(reaction);
}
}
} else if ((reaction.f & DIRTY) !== 0) {
// `signal` might now be dirty, as a result of calling `check_dirtiness` and/or `update_derived`
return true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from '../../test';

export default test({
html: `<button>00</button>`,

async test({ assert, target }) {
const btn = target.querySelector('button');
await btn?.click();

assert.htmlEqual(target.innerHTML, `<button>01</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let shouldShow01 = $state(false);

let der1 = $derived(shouldShow01)
// der2 must depend on der1 and its output shouldn't change
let der2 = $derived(typeof der1 === "string");
let der3 = $derived(der2 ? "1" : "0");
// der3 must be read before der1
let der4 = $derived(der3 + (der1 ? "1" : "0"));
</script>

<button onclick={() => (shouldShow01 = true)}>{der4}</button>
Loading