Skip to content

Commit 909f882

Browse files
chore: simpler untracking (#12049)
* chore: get rid of current_untracking * simplify * remove incorrect comment * only apply unowned check to user effects - less work that way * tweak --------- Co-authored-by: Simon Holthausen <[email protected]>
1 parent a1b6cc6 commit 909f882

File tree

3 files changed

+23
-49
lines changed

3 files changed

+23
-49
lines changed

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
current_component_context,
44
current_effect,
55
current_reaction,
6-
current_untracking,
76
destroy_effect_children,
87
dev_current_component_function,
98
execute_effect,
@@ -12,10 +11,10 @@ import {
1211
is_flushing_effect,
1312
remove_reactions,
1413
schedule_effect,
14+
set_current_reaction,
1515
set_is_destroying_effect,
1616
set_is_flushing_effect,
1717
set_signal_status,
18-
set_untracking,
1918
untrack
2019
} from '../runtime.js';
2120
import {
@@ -47,6 +46,10 @@ export function validate_effect(rune) {
4746
e.effect_orphan(rune);
4847
}
4948

49+
if (current_reaction !== null && (current_reaction.f & UNOWNED) !== 0) {
50+
e.effect_in_unowned_derived();
51+
}
52+
5053
if (is_destroying_effect) {
5154
e.effect_in_teardown(rune);
5255
}
@@ -119,20 +122,15 @@ function create_effect(type, fn, sync) {
119122
effect.dom === null &&
120123
effect.teardown === null;
121124

122-
if (!inert && current_reaction !== null && !is_root) {
123-
var flags = current_reaction.f;
124-
if ((flags & DERIVED) !== 0) {
125-
if ((flags & UNOWNED) !== 0) {
126-
e.effect_in_unowned_derived();
127-
}
128-
// If we are inside a derived, then we also need to attach the
129-
// effect to the parent effect too.
130-
if (current_effect !== null) {
131-
push_effect(effect, current_effect);
132-
}
125+
if (!inert && !is_root) {
126+
if (current_effect !== null) {
127+
push_effect(effect, current_effect);
133128
}
134129

135-
push_effect(effect, current_reaction);
130+
// if we're in a derived, add the effect there too
131+
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) {
132+
push_effect(effect, current_reaction);
133+
}
136134
}
137135

138136
return effect;
@@ -143,18 +141,11 @@ function create_effect(type, fn, sync) {
143141
* @returns {boolean}
144142
*/
145143
export function effect_tracking() {
146-
if (current_untracking) {
144+
if (current_reaction === null) {
147145
return false;
148146
}
149-
if (current_reaction && (current_reaction.f & DERIVED) !== 0) {
150-
return (current_reaction.f & UNOWNED) === 0;
151-
}
152-
153-
if (current_effect) {
154-
return (current_effect.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0;
155-
}
156147

157-
return false;
148+
return (current_reaction.f & UNOWNED) === 0;
158149
}
159150

160151
/**
@@ -320,14 +311,14 @@ export function execute_effect_teardown(effect) {
320311
var teardown = effect.teardown;
321312
if (teardown !== null) {
322313
const previously_destroying_effect = is_destroying_effect;
323-
const previous_untracking = current_untracking;
314+
const previous_reaction = current_reaction;
324315
set_is_destroying_effect(true);
325-
set_untracking(true);
316+
set_current_reaction(null);
326317
try {
327318
teardown.call(null);
328319
} finally {
329320
set_is_destroying_effect(previously_destroying_effect);
330-
set_untracking(previous_untracking);
321+
set_current_reaction(previous_reaction);
331322
}
332323
}
333324
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
current_dependencies,
66
current_effect,
77
current_untracked_writes,
8-
current_untracking,
98
get,
109
is_batching_effect,
1110
is_runes,
@@ -87,7 +86,6 @@ export function set(signal, value) {
8786
var initialized = signal.v !== UNINITIALIZED;
8887

8988
if (
90-
!current_untracking &&
9189
initialized &&
9290
current_reaction !== null &&
9391
is_runes() &&

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ export function set_is_destroying_effect(value) {
6363
is_destroying_effect = value;
6464
}
6565

66-
/** @param {boolean} value */
67-
export function set_untracking(value) {
68-
current_untracking = value;
69-
}
70-
7166
// Used for $inspect
7267
export let is_batching_effect = false;
7368
let is_inspecting_signal = false;
@@ -119,10 +114,7 @@ export function set_last_inspected_signal(signal) {
119114
last_inspected_signal = signal;
120115
}
121116

122-
/** If `true`, `get`ting the signal should not register it as a dependency */
123-
export let current_untracking = false;
124-
125-
/** @type {number} */
117+
/** @type {number} Used by sources and deriveds for handling updates to unowned deriveds */
126118
let current_version = 0;
127119

128120
// If we are working with a get() chain that has no active container,
@@ -351,14 +343,12 @@ export function execute_reaction_fn(signal) {
351343
const previous_untracked_writes = current_untracked_writes;
352344
const previous_reaction = current_reaction;
353345
const previous_skip_reaction = current_skip_reaction;
354-
const previous_untracking = current_untracking;
355346

356347
current_dependencies = /** @type {null | import('#client').Value[]} */ (null);
357348
current_dependencies_index = 0;
358349
current_untracked_writes = null;
359-
current_reaction = signal;
350+
current_reaction = (signal.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? signal : null;
360351
current_skip_reaction = !is_flushing_effect && (signal.f & UNOWNED) !== 0;
361-
current_untracking = false;
362352

363353
try {
364354
let res = /** @type {Function} */ (0, signal.fn)();
@@ -429,7 +419,6 @@ export function execute_reaction_fn(signal) {
429419
current_untracked_writes = previous_untracked_writes;
430420
current_reaction = previous_reaction;
431421
current_skip_reaction = previous_skip_reaction;
432-
current_untracking = previous_untracking;
433422
}
434423
}
435424

@@ -820,11 +809,7 @@ export function get(signal) {
820809
}
821810

822811
// Register the dependency on the current reaction signal.
823-
if (
824-
current_reaction !== null &&
825-
(current_reaction.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 &&
826-
!current_untracking
827-
) {
812+
if (current_reaction !== null) {
828813
const unowned = (current_reaction.f & UNOWNED) !== 0;
829814
const dependencies = current_reaction.deps;
830815
if (
@@ -967,12 +952,12 @@ export function mark_reactions(signal, to_status, force_schedule) {
967952
* @returns {T}
968953
*/
969954
export function untrack(fn) {
970-
const previous_untracking = current_untracking;
955+
const previous_reaction = current_reaction;
971956
try {
972-
current_untracking = true;
957+
current_reaction = null;
973958
return fn();
974959
} finally {
975-
current_untracking = previous_untracking;
960+
current_reaction = previous_reaction;
976961
}
977962
}
978963

0 commit comments

Comments
 (0)