From 59486beb29d5bf385cf008d1742faf49a3d9b6bd Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 17 Jun 2024 01:57:38 -0700 Subject: [PATCH] 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 --- .../internal/client/reactivity/deriveds.js | 8 ++----- .../svelte/src/internal/client/runtime.js | 22 ++++++------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/packages/svelte/src/internal/client/reactivity/deriveds.js b/packages/svelte/src/internal/client/reactivity/deriveds.js index d50b335a7b..7894ea9f0c 100644 --- a/packages/svelte/src/internal/client/reactivity/deriveds.js +++ b/packages/svelte/src/internal/client/reactivity/deriveds.js @@ -86,7 +86,7 @@ function destroy_derived_children(signal) { /** * @param {import('#client').Derived} derived * @param {boolean} force_schedule - * @returns {boolean} + * @returns {void} */ export function update_derived(derived, force_schedule) { var previous_updating_derived = updating_derived; @@ -102,9 +102,7 @@ export function update_derived(derived, force_schedule) { set_signal_status(derived, status); - var is_equal = derived.equals(value); - - if (!is_equal) { + if (!derived.equals(value)) { derived.version = increment_version(); derived.v = value; mark_reactions(derived, DIRTY, force_schedule); @@ -113,8 +111,6 @@ export function update_derived(derived, force_schedule) { for (var fn of /** @type {import('#client').DerivedDebug} */ (derived).inspect) fn(); } } - - return is_equal; } /** diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 0b8c522140..6e9b00dfd5 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -208,30 +208,28 @@ export function batch_inspect(target, prop, receiver) { export function check_dirtiness(reaction) { var flags = reaction.f; var is_dirty = (flags & DIRTY) !== 0; - var is_unowned = (flags & UNOWNED) !== 0; - // If we are unowned, we still need to ensure that we update our version to that - // of our dependencies. - if (is_dirty && !is_unowned) { + if (is_dirty) { return true; } + var is_unowned = (flags & UNOWNED) !== 0; var is_disconnected = (flags & DISCONNECTED) !== 0; - if ((flags & MAYBE_DIRTY) !== 0 || (is_dirty && is_unowned)) { + if ((flags & MAYBE_DIRTY) !== 0) { var dependencies = reaction.deps; if (dependencies !== null) { var length = dependencies.length; - var is_equal; var reactions; for (var i = 0; i < length; i++) { var dependency = dependencies[i]; if (!is_dirty && check_dirtiness(/** @type {import('#client').Derived} */ (dependency))) { - is_equal = update_derived(/** @type {import('#client').Derived} **/ (dependency), true); + update_derived(/** @type {import('#client').Derived} **/ (dependency), true); } + var version = dependency.version; if (is_unowned) { @@ -239,21 +237,15 @@ 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. - if (version > /** @type {import('#client').Derived} */ (reaction).version) { - return !is_equal; + return true; } if (!current_skip_reaction && !dependency?.reactions?.includes(reaction)) { // If we are working with an unowned signal as part of an effect (due to !current_skip_reaction) // and the version hasn't changed, we still need to check that this reaction // if linked to the dependency source – otherwise future updates will not be caught. - reactions = dependency.reactions; - if (reactions === null) { - dependency.reactions = [reaction]; - } else { - reactions.push(reaction); - } + (dependency.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`