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 <simon.holthausen@vercel.com>
pull/12051/head
Rich Harris 5 months ago committed by GitHub
parent f5f38796ba
commit 59486beb29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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;
}
/**

@ -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`

Loading…
Cancel
Save