Skip to content

Commit 00d0c42

Browse files
committed
fix: ensure reactions of non-updating deriveds have their version synced
1 parent 7ce2dfc commit 00d0c42

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

packages/svelte/src/internal/client/reactivity/deriveds.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { Derived, Effect } from '#client' */
1+
/** @import { Derived, Effect, Value } from '#client' */
22
import { DEV } from 'esm-env';
33
import { CLEAN, DERIVED, DIRTY, EFFECT_HAS_DERIVED, MAYBE_DIRTY, UNOWNED } from '../constants.js';
44
import {
@@ -151,6 +151,25 @@ function execute_derived(derived) {
151151
return value;
152152
}
153153

154+
/**
155+
* @param {Value} value
156+
*/
157+
function sync_reaction_versions(value) {
158+
var reactions = value.reactions;
159+
160+
if (reactions !== null) {
161+
for (var i = 0; i < reactions.length; i++) {
162+
var reaction = reactions[i];
163+
if ((value.f & UNOWNED) === 0 && value.wv > reaction.wv) {
164+
reaction.wv = value.wv;
165+
}
166+
if ((reaction.f & DERIVED) !== 0) {
167+
sync_reaction_versions(/** @type {Derived} */ (reaction));
168+
}
169+
}
170+
}
171+
}
172+
154173
/**
155174
* @param {Derived} derived
156175
* @returns {void}
@@ -165,5 +184,7 @@ export function update_derived(derived) {
165184
if (!derived.equals(value)) {
166185
derived.v = value;
167186
derived.wv = increment_write_version();
187+
} else {
188+
sync_reaction_versions(derived);
168189
}
169190
}

packages/svelte/src/internal/client/runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export function check_dirtiness(reaction) {
206206
update_derived(/** @type {Derived} */ (dependency));
207207
}
208208

209-
if (dependency.wv > reaction.wv) {
209+
if (dependency.wv > reaction.wv || (reaction.f & DIRTY) !== 0) {
210210
return true;
211211
}
212212
}

packages/svelte/tests/runtime-runes/samples/derived-write-read-write-read/main.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
// resulting in the same value should not prevent pending render effects from updating
1111
z;
1212
y = 0;
13-
}}>{z}</button>
13+
}}>{z}</button>

0 commit comments

Comments
 (0)