Skip to content

Commit 3f2dc2e

Browse files
authored
chore: tweak reactivity (#12266)
* chore: tweak reactivity * rename stuff * var etc * lint
1 parent ebf72be commit 3f2dc2e

File tree

4 files changed

+64
-65
lines changed

4 files changed

+64
-65
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
set_signal_status,
77
mark_reactions,
88
current_skip_reaction,
9-
execute_reaction_fn,
9+
update_reaction,
1010
destroy_effect_children,
1111
increment_version
1212
} from '../runtime.js';
@@ -87,7 +87,7 @@ export function update_derived(derived) {
8787
var previous_updating_derived = updating_derived;
8888
updating_derived = true;
8989
destroy_derived_children(derived);
90-
var value = execute_reaction_fn(derived);
90+
var value = update_reaction(derived);
9191
updating_derived = previous_updating_derived;
9292

9393
var status =

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
current_reaction,
66
destroy_effect_children,
77
dev_current_component_function,
8-
execute_effect,
8+
update_effect,
99
get,
1010
is_destroying_effect,
1111
is_flushing_effect,
@@ -106,7 +106,7 @@ function create_effect(type, fn, sync, push = true) {
106106

107107
try {
108108
set_is_flushing_effect(true);
109-
execute_effect(effect);
109+
update_effect(effect);
110110
effect.f |= EFFECT_RAN;
111111
} finally {
112112
set_is_flushing_effect(previously_flushing_effect);
@@ -267,7 +267,7 @@ export function legacy_pre_effect_reset() {
267267
var effect = token.effect;
268268

269269
if (check_dirtiness(effect)) {
270-
execute_effect(effect);
270+
update_effect(effect);
271271
}
272272

273273
token.ran = false;
@@ -495,7 +495,7 @@ function resume_children(effect, local) {
495495
// If a dependency of this effect changed while it was paused,
496496
// apply the change now
497497
if (check_dirtiness(effect)) {
498-
execute_effect(effect);
498+
update_effect(effect);
499499
}
500500

501501
var child = effect.first;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
set_signal_status,
1414
untrack,
1515
increment_version,
16-
execute_effect,
16+
update_effect,
1717
inspect_effects
1818
} from '../runtime.js';
1919
import { equals, safe_equals } from './equality.js';
@@ -123,7 +123,7 @@ export function set(source, value) {
123123

124124
if (DEV) {
125125
for (const effect of inspect_effects) {
126-
execute_effect(effect);
126+
update_effect(effect);
127127
}
128128

129129
inspect_effects.clear();

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

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,17 @@ export function check_dirtiness(reaction) {
180180
update_derived(/** @type {import('#client').Derived} **/ (dependency));
181181
}
182182

183-
var version = dependency.version;
183+
if ((reaction.f & DIRTY) !== 0) {
184+
// `reaction` might now be dirty, as a result of calling `update_derived`
185+
return true;
186+
}
184187

185188
if (is_unowned) {
186189
// If we're working with an unowned derived signal, then we need to check
187190
// if our dependency write version is higher. If it is then we can assume
188191
// that state has changed to a newer version and thus this unowned signal
189192
// is also dirty.
190-
if (version > /** @type {import('#client').Derived} */ (reaction).version) {
193+
if (dependency.version > /** @type {import('#client').Derived} */ (reaction).version) {
191194
return true;
192195
}
193196

@@ -197,14 +200,11 @@ export function check_dirtiness(reaction) {
197200
// if linked to the dependency source – otherwise future updates will not be caught.
198201
(dependency.reactions ??= []).push(reaction);
199202
}
200-
} else if ((reaction.f & DIRTY) !== 0) {
201-
// `signal` might now be dirty, as a result of calling `check_dirtiness` and/or `update_derived`
202-
return true;
203203
} else if (is_disconnected) {
204204
// It might be that the derived was was dereferenced from its dependencies but has now come alive again.
205205
// In thise case, we need to re-attach it to the graph and mark it dirty if any of its dependencies have
206206
// changed since.
207-
if (version > /** @type {import('#client').Derived} */ (reaction).version) {
207+
if (dependency.version > /** @type {import('#client').Derived} */ (reaction).version) {
208208
is_dirty = true;
209209
}
210210

@@ -292,49 +292,49 @@ function handle_error(error, effect, component_context) {
292292

293293
/**
294294
* @template V
295-
* @param {import('#client').Reaction} signal
295+
* @param {import('#client').Reaction} reaction
296296
* @returns {V}
297297
*/
298-
export function execute_reaction_fn(signal) {
299-
const previous_dependencies = current_dependencies;
300-
const previous_dependencies_index = current_dependencies_index;
301-
const previous_untracked_writes = current_untracked_writes;
302-
const previous_reaction = current_reaction;
303-
const previous_skip_reaction = current_skip_reaction;
298+
export function update_reaction(reaction) {
299+
var previous_dependencies = current_dependencies;
300+
var previous_dependencies_index = current_dependencies_index;
301+
var previous_untracked_writes = current_untracked_writes;
302+
var previous_reaction = current_reaction;
303+
var previous_skip_reaction = current_skip_reaction;
304304

305305
current_dependencies = /** @type {null | import('#client').Value[]} */ (null);
306306
current_dependencies_index = 0;
307307
current_untracked_writes = null;
308-
current_reaction = (signal.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? signal : null;
309-
current_skip_reaction = !is_flushing_effect && (signal.f & UNOWNED) !== 0;
308+
current_reaction = (reaction.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null;
309+
current_skip_reaction = !is_flushing_effect && (reaction.f & UNOWNED) !== 0;
310310

311311
try {
312-
let res = /** @type {Function} */ (0, signal.fn)();
313-
let dependencies = /** @type {import('#client').Value<unknown>[]} **/ (signal.deps);
312+
var result = /** @type {Function} */ (0, reaction.fn)();
313+
var dependencies = /** @type {import('#client').Value<unknown>[]} **/ (reaction.deps);
314+
314315
if (current_dependencies !== null) {
315-
let i;
316+
var dependency;
317+
var i;
318+
316319
if (dependencies !== null) {
317-
const deps_length = dependencies.length;
318-
// Include any dependencies up until the current_dependencies_index.
319-
const full_current_dependencies =
320+
var deps_length = dependencies.length;
321+
322+
/** All dependencies of the reaction, including those tracked on the previous run */
323+
var array =
320324
current_dependencies_index === 0
321325
? current_dependencies
322326
: dependencies.slice(0, current_dependencies_index).concat(current_dependencies);
323-
const current_dep_length = full_current_dependencies.length;
327+
324328
// If we have more than 16 elements in the array then use a Set for faster performance
325329
// TODO: evaluate if we should always just use a Set or not here?
326-
const full_current_dependencies_set =
327-
current_dep_length > 16 && deps_length - current_dependencies_index > 1
328-
? new Set(full_current_dependencies)
329-
: null;
330+
var set =
331+
array.length > 16 && deps_length - current_dependencies_index > 1 ? new Set(array) : null;
332+
330333
for (i = current_dependencies_index; i < deps_length; i++) {
331-
const dependency = dependencies[i];
332-
if (
333-
full_current_dependencies_set !== null
334-
? !full_current_dependencies_set.has(dependency)
335-
: !full_current_dependencies.includes(dependency)
336-
) {
337-
remove_reaction(signal, dependency);
334+
dependency = dependencies[i];
335+
336+
if (set !== null ? !set.has(dependency) : !array.includes(dependency)) {
337+
remove_reaction(reaction, dependency);
338338
}
339339
}
340340
}
@@ -345,28 +345,32 @@ export function execute_reaction_fn(signal) {
345345
dependencies[current_dependencies_index + i] = current_dependencies[i];
346346
}
347347
} else {
348-
signal.deps = /** @type {import('#client').Value<V>[]} **/ (
348+
reaction.deps = /** @type {import('#client').Value<V>[]} **/ (
349349
dependencies = current_dependencies
350350
);
351351
}
352352

353353
if (!current_skip_reaction) {
354354
for (i = current_dependencies_index; i < dependencies.length; i++) {
355-
const dependency = dependencies[i];
356-
const reactions = dependency.reactions;
355+
dependency = dependencies[i];
356+
var reactions = dependency.reactions;
357357

358358
if (reactions === null) {
359-
dependency.reactions = [signal];
360-
} else if (reactions[reactions.length - 1] !== signal && !reactions.includes(signal)) {
361-
reactions.push(signal);
359+
dependency.reactions = [reaction];
360+
} else if (
361+
reactions[reactions.length - 1] !== reaction &&
362+
!reactions.includes(reaction)
363+
) {
364+
reactions.push(reaction);
362365
}
363366
}
364367
}
365368
} else if (dependencies !== null && current_dependencies_index < dependencies.length) {
366-
remove_reactions(signal, current_dependencies_index);
369+
remove_reactions(reaction, current_dependencies_index);
367370
dependencies.length = current_dependencies_index;
368371
}
369-
return res;
372+
373+
return result;
370374
} finally {
371375
current_dependencies = previous_dependencies;
372376
current_dependencies_index = previous_dependencies_index;
@@ -456,7 +460,7 @@ export function destroy_effect_children(signal, remove_dom = false) {
456460
* @param {import('#client').Effect} effect
457461
* @returns {void}
458462
*/
459-
export function execute_effect(effect) {
463+
export function update_effect(effect) {
460464
var flags = effect.f;
461465

462466
if ((flags & DESTROYED) !== 0) {
@@ -484,7 +488,7 @@ export function execute_effect(effect) {
484488
}
485489

486490
execute_effect_teardown(effect);
487-
var teardown = execute_reaction_fn(effect);
491+
var teardown = update_reaction(effect);
488492
effect.teardown = typeof teardown === 'function' ? teardown : null;
489493
} catch (error) {
490494
handle_error(/** @type {Error} */ (error), effect, current_component_context);
@@ -552,12 +556,12 @@ function flush_queued_effects(effects) {
552556
var effect = effects[i];
553557

554558
if ((effect.f & (DESTROYED | INERT)) === 0 && check_dirtiness(effect)) {
555-
execute_effect(effect);
559+
update_effect(effect);
556560

557561
// Effects with no dependencies or teardown do not get added to the effect tree.
558562
// Deferred effects (e.g. `$effect(...)`) _are_ added to the tree because we
559563
// don't know if we need to keep them until they are executed. Doing the check
560-
// here (rather than in `execute_effect`) allows us to skip the work for
564+
// here (rather than in `update_effect`) allows us to skip the work for
561565
// immediate effects.
562566
if (effect.deps === null && effect.first === null && effect.nodes === null) {
563567
if (effect.teardown === null) {
@@ -643,7 +647,7 @@ function process_effects(effect, collected_effects) {
643647

644648
if ((flags & RENDER_EFFECT) !== 0) {
645649
if (!is_branch && check_dirtiness(current_effect)) {
646-
execute_effect(current_effect);
650+
update_effect(current_effect);
647651
// Child might have been mutated since running the effect
648652
child = current_effect.first;
649653
}
@@ -843,11 +847,11 @@ export function invalidate_inner_signals(fn) {
843847

844848
/**
845849
* @param {import('#client').Value} signal
846-
* @param {number} to_status should be DIRTY or MAYBE_DIRTY
850+
* @param {number} status should be DIRTY or MAYBE_DIRTY
847851
* @param {boolean} force_schedule
848852
* @returns {void}
849853
*/
850-
export function mark_reactions(signal, to_status, force_schedule) {
854+
export function mark_reactions(signal, status, force_schedule) {
851855
var reactions = signal.reactions;
852856
if (reactions === null) return;
853857

@@ -870,16 +874,11 @@ export function mark_reactions(signal, to_status, force_schedule) {
870874
continue;
871875
}
872876

873-
set_signal_status(reaction, to_status);
874-
875-
// If the signal is not clean, then skip over it – with the exception of unowned signals that
876-
// are already maybe dirty. Unowned signals might be dirty because they are not captured as part of an
877-
// effect.
878-
var maybe_dirty = (flags & MAYBE_DIRTY) !== 0;
879-
var unowned = (flags & UNOWNED) !== 0;
877+
set_signal_status(reaction, status);
880878

881-
if ((flags & CLEAN) !== 0 || (maybe_dirty && unowned)) {
882-
if ((reaction.f & DERIVED) !== 0) {
879+
// If the signal a) was previously clean or b) is an unowned derived, then mark it
880+
if ((flags & (CLEAN | UNOWNED)) !== 0) {
881+
if ((flags & DERIVED) !== 0) {
883882
mark_reactions(
884883
/** @type {import('#client').Derived} */ (reaction),
885884
MAYBE_DIRTY,

0 commit comments

Comments
 (0)