Skip to content

Commit 59486be

Browse files
chore: simplify deriveds a tiny bit (#12048)
* fix: increment derived versions when updating * we only need to increment version when setting sources and updating deriveds * no tests fail if we remove this code * codegolf --------- Co-authored-by: Simon Holthausen <[email protected]>
1 parent f5f3879 commit 59486be

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function destroy_derived_children(signal) {
8686
/**
8787
* @param {import('#client').Derived} derived
8888
* @param {boolean} force_schedule
89-
* @returns {boolean}
89+
* @returns {void}
9090
*/
9191
export function update_derived(derived, force_schedule) {
9292
var previous_updating_derived = updating_derived;
@@ -102,9 +102,7 @@ export function update_derived(derived, force_schedule) {
102102

103103
set_signal_status(derived, status);
104104

105-
var is_equal = derived.equals(value);
106-
107-
if (!is_equal) {
105+
if (!derived.equals(value)) {
108106
derived.version = increment_version();
109107
derived.v = value;
110108
mark_reactions(derived, DIRTY, force_schedule);
@@ -113,8 +111,6 @@ export function update_derived(derived, force_schedule) {
113111
for (var fn of /** @type {import('#client').DerivedDebug} */ (derived).inspect) fn();
114112
}
115113
}
116-
117-
return is_equal;
118114
}
119115

120116
/**

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -208,52 +208,44 @@ export function batch_inspect(target, prop, receiver) {
208208
export function check_dirtiness(reaction) {
209209
var flags = reaction.f;
210210
var is_dirty = (flags & DIRTY) !== 0;
211-
var is_unowned = (flags & UNOWNED) !== 0;
212211

213-
// If we are unowned, we still need to ensure that we update our version to that
214-
// of our dependencies.
215-
if (is_dirty && !is_unowned) {
212+
if (is_dirty) {
216213
return true;
217214
}
218215

216+
var is_unowned = (flags & UNOWNED) !== 0;
219217
var is_disconnected = (flags & DISCONNECTED) !== 0;
220218

221-
if ((flags & MAYBE_DIRTY) !== 0 || (is_dirty && is_unowned)) {
219+
if ((flags & MAYBE_DIRTY) !== 0) {
222220
var dependencies = reaction.deps;
223221

224222
if (dependencies !== null) {
225223
var length = dependencies.length;
226-
var is_equal;
227224
var reactions;
228225

229226
for (var i = 0; i < length; i++) {
230227
var dependency = dependencies[i];
231228

232229
if (!is_dirty && check_dirtiness(/** @type {import('#client').Derived} */ (dependency))) {
233-
is_equal = update_derived(/** @type {import('#client').Derived} **/ (dependency), true);
230+
update_derived(/** @type {import('#client').Derived} **/ (dependency), true);
234231
}
232+
235233
var version = dependency.version;
236234

237235
if (is_unowned) {
238236
// If we're working with an unowned derived signal, then we need to check
239237
// if our dependency write version is higher. If it is then we can assume
240238
// that state has changed to a newer version and thus this unowned signal
241239
// is also dirty.
242-
243240
if (version > /** @type {import('#client').Derived} */ (reaction).version) {
244-
return !is_equal;
241+
return true;
245242
}
246243

247244
if (!current_skip_reaction && !dependency?.reactions?.includes(reaction)) {
248245
// If we are working with an unowned signal as part of an effect (due to !current_skip_reaction)
249246
// and the version hasn't changed, we still need to check that this reaction
250247
// if linked to the dependency source – otherwise future updates will not be caught.
251-
reactions = dependency.reactions;
252-
if (reactions === null) {
253-
dependency.reactions = [reaction];
254-
} else {
255-
reactions.push(reaction);
256-
}
248+
(dependency.reactions ??= []).push(reaction);
257249
}
258250
} else if ((reaction.f & DIRTY) !== 0) {
259251
// `signal` might now be dirty, as a result of calling `check_dirtiness` and/or `update_derived`

0 commit comments

Comments
 (0)