Skip to content

Commit

Permalink
fix: reset dependency read versions after reaction execution (#14964)
Browse files Browse the repository at this point in the history
* fix: reset dependency read versions after reaction execution

* fix: reset dependency read versions after reaction execution

* fix: reset dependency read versions after reaction execution

* fix: reset dependency read versions after reaction execution

* fix: reset dependency read versions after reaction execution

* chore: add test

* changeset

---------

Co-authored-by: paoloricciuti <ricciutipaolo@gmail.com>
Co-authored-by: Rich Harris <rich.harris@vercel.com>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent b7400ae commit bdc0200
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-chefs-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: reset dependency read versions after reaction execution
8 changes: 8 additions & 0 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ export function update_reaction(reaction) {
deps.length = skipped_deps;
}

// If we are returning to an previous reaction then
// we need to increment the read version to ensure that
// any dependencies in this reaction aren't marked with
// the same version
if (previous_reaction !== null) {
read_version++;
}

return result;
} finally {
new_deps = previous_deps;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const { label = 0, size = 0 } = $props();
const title = $derived(size.toString());
</script>

<p {title}>{label}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ok, test } from '../../test';
import { flushSync } from 'svelte';

export default test({
html: `<button></button><p title="0">0</p>`,

async test({ assert, target }) {
const p = target.querySelector('p');
const btn = target.querySelector('button');
flushSync(() => {
btn?.click();
});
assert.equal(p?.innerHTML, '1');
flushSync(() => {
btn?.click();
});
assert.equal(p?.innerHTML, '2');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
import Component from "./Component.svelte";
let props = $state({
label: 0,
size: 0,
});
let filteredProps = $state();
$effect.pre(() => {
filteredProps = $state.snapshot(props);
});
</script>

<button onclick={()=>props.label++}></button>

<Component {...filteredProps} />

0 comments on commit bdc0200

Please sign in to comment.