Skip to content

Commit d117320

Browse files
committed
refactor: extract setting derived status to helper, apply to sources.js
1 parent 3268f7d commit d117320

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
increment_write_version,
2323
set_active_effect,
2424
push_reaction_value,
25-
is_destroying_effect
25+
is_destroying_effect,
26+
update_derived_status
2627
} from '../runtime.js';
2728
import { equals, safe_equals } from './equality.js';
2829
import * as e from '../errors.js';
@@ -386,13 +387,7 @@ export function update_derived(derived) {
386387
}
387388
}
388389

389-
// Only mark as MAYBE_DIRTY if disconnected AND has dependencies.
390-
// Deriveds with no deps can never become dirty from dependency changes.
391390
if (batch_values === null || derived.deps === null) {
392-
if ((derived.f & CONNECTED) === 0 && derived.deps !== null) {
393-
set_signal_status(derived, MAYBE_DIRTY);
394-
} else {
395-
set_signal_status(derived, CLEAN);
396-
}
391+
update_derived_status(derived);
397392
}
398393
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
is_destroying_effect,
1717
push_reaction_value,
1818
set_is_updating_effect,
19-
is_updating_effect
19+
is_updating_effect,
20+
update_derived_status
2021
} from '../runtime.js';
2122
import { equals, safe_equals } from './equality.js';
2223
import {
@@ -218,12 +219,14 @@ export function internal_set(source, value) {
218219
}
219220

220221
if ((source.f & DERIVED) !== 0) {
222+
const derived = /** @type {Derived} */ (source);
223+
221224
// if we are assigning to a dirty derived we set it to clean/maybe dirty but we also eagerly execute it to track the dependencies
222225
if ((source.f & DIRTY) !== 0) {
223-
execute_derived(/** @type {Derived} */ (source));
226+
execute_derived(derived);
224227
}
225228

226-
set_signal_status(source, (source.f & CONNECTED) !== 0 ? CLEAN : MAYBE_DIRTY);
229+
update_derived_status(derived);
227230
}
228231

229232
source.wv = increment_write_version();

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ export function is_dirty(reaction) {
185185
}
186186

187187
if (
188-
(flags & CONNECTED) !== 0 &&
188+
(flags & DERIVED) !== 0 &&
189189
// During time traveling we don't want to reset the status so that
190190
// traversal of the graph in the other batches still happens
191-
batch_values === null
191+
(batch_values === null || reaction.deps === null)
192192
) {
193-
set_signal_status(reaction, CLEAN);
193+
update_derived_status(/** @type {Derived} */ (reaction));
194194
}
195195
}
196196

@@ -732,6 +732,19 @@ export function set_signal_status(signal, status) {
732732
signal.f = (signal.f & STATUS_MASK) | status;
733733
}
734734

735+
/**
736+
* Set a derived's status to CLEAN or MAYBE_DIRTY based on its connection state.
737+
* @param {Derived} derived
738+
*/
739+
export function update_derived_status(derived) {
740+
// Only mark as MAYBE_DIRTY if disconnected and has dependencies.
741+
if ((derived.f & CONNECTED) !== 0 || derived.deps === null) {
742+
set_signal_status(derived, CLEAN);
743+
} else {
744+
set_signal_status(derived, MAYBE_DIRTY);
745+
}
746+
}
747+
735748
/**
736749
* @param {Record<string | symbol, unknown>} obj
737750
* @param {Array<string | symbol>} keys

0 commit comments

Comments
 (0)