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 <simon.holthausen@vercel.com>
pull/12058/head
Rich Harris 3 weeks ago committed by GitHub
parent a1b6cc68d9
commit 909f88287a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -3,7 +3,6 @@ import {
current_component_context, current_component_context,
current_effect, current_effect,
current_reaction, current_reaction,
current_untracking,
destroy_effect_children, destroy_effect_children,
dev_current_component_function, dev_current_component_function,
execute_effect, execute_effect,
@ -12,10 +11,10 @@ import {
is_flushing_effect, is_flushing_effect,
remove_reactions, remove_reactions,
schedule_effect, schedule_effect,
set_current_reaction,
set_is_destroying_effect, set_is_destroying_effect,
set_is_flushing_effect, set_is_flushing_effect,
set_signal_status, set_signal_status,
set_untracking,
untrack untrack
} from '../runtime.js'; } from '../runtime.js';
import { import {
@ -47,6 +46,10 @@ export function validate_effect(rune) {
e.effect_orphan(rune); e.effect_orphan(rune);
} }
if (current_reaction !== null && (current_reaction.f & UNOWNED) !== 0) {
e.effect_in_unowned_derived();
}
if (is_destroying_effect) { if (is_destroying_effect) {
e.effect_in_teardown(rune); e.effect_in_teardown(rune);
} }
@ -119,20 +122,15 @@ function create_effect(type, fn, sync) {
effect.dom === null && effect.dom === null &&
effect.teardown === null; effect.teardown === null;
if (!inert && current_reaction !== null && !is_root) { if (!inert && !is_root) {
var flags = current_reaction.f; if (current_effect !== null) {
if ((flags & DERIVED) !== 0) { push_effect(effect, current_effect);
if ((flags & UNOWNED) !== 0) {
e.effect_in_unowned_derived();
}
// If we are inside a derived, then we also need to attach the
// effect to the parent effect too.
if (current_effect !== null) {
push_effect(effect, current_effect);
}
} }
push_effect(effect, current_reaction); // if we're in a derived, add the effect there too
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) {
push_effect(effect, current_reaction);
}
} }
return effect; return effect;
@ -143,18 +141,11 @@ function create_effect(type, fn, sync) {
* @returns {boolean} * @returns {boolean}
*/ */
export function effect_tracking() { export function effect_tracking() {
if (current_untracking) { if (current_reaction === null) {
return false; return false;
} }
if (current_reaction && (current_reaction.f & DERIVED) !== 0) {
return (current_reaction.f & UNOWNED) === 0;
}
if (current_effect) {
return (current_effect.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0;
}
return false; return (current_reaction.f & UNOWNED) === 0;
} }
/** /**
@ -320,14 +311,14 @@ export function execute_effect_teardown(effect) {
var teardown = effect.teardown; var teardown = effect.teardown;
if (teardown !== null) { if (teardown !== null) {
const previously_destroying_effect = is_destroying_effect; const previously_destroying_effect = is_destroying_effect;
const previous_untracking = current_untracking; const previous_reaction = current_reaction;
set_is_destroying_effect(true); set_is_destroying_effect(true);
set_untracking(true); set_current_reaction(null);
try { try {
teardown.call(null); teardown.call(null);
} finally { } finally {
set_is_destroying_effect(previously_destroying_effect); set_is_destroying_effect(previously_destroying_effect);
set_untracking(previous_untracking); set_current_reaction(previous_reaction);
} }
} }
} }

@ -5,7 +5,6 @@ import {
current_dependencies, current_dependencies,
current_effect, current_effect,
current_untracked_writes, current_untracked_writes,
current_untracking,
get, get,
is_batching_effect, is_batching_effect,
is_runes, is_runes,
@ -87,7 +86,6 @@ export function set(signal, value) {
var initialized = signal.v !== UNINITIALIZED; var initialized = signal.v !== UNINITIALIZED;
if ( if (
!current_untracking &&
initialized && initialized &&
current_reaction !== null && current_reaction !== null &&
is_runes() && is_runes() &&

@ -63,11 +63,6 @@ export function set_is_destroying_effect(value) {
is_destroying_effect = value; is_destroying_effect = value;
} }
/** @param {boolean} value */
export function set_untracking(value) {
current_untracking = value;
}
// Used for $inspect // Used for $inspect
export let is_batching_effect = false; export let is_batching_effect = false;
let is_inspecting_signal = false; let is_inspecting_signal = false;
@ -119,10 +114,7 @@ export function set_last_inspected_signal(signal) {
last_inspected_signal = signal; last_inspected_signal = signal;
} }
/** If `true`, `get`ting the signal should not register it as a dependency */ /** @type {number} Used by sources and deriveds for handling updates to unowned deriveds */
export let current_untracking = false;
/** @type {number} */
let current_version = 0; let current_version = 0;
// If we are working with a get() chain that has no active container, // If we are working with a get() chain that has no active container,
@ -351,14 +343,12 @@ export function execute_reaction_fn(signal) {
const previous_untracked_writes = current_untracked_writes; const previous_untracked_writes = current_untracked_writes;
const previous_reaction = current_reaction; const previous_reaction = current_reaction;
const previous_skip_reaction = current_skip_reaction; const previous_skip_reaction = current_skip_reaction;
const previous_untracking = current_untracking;
current_dependencies = /** @type {null | import('#client').Value[]} */ (null); current_dependencies = /** @type {null | import('#client').Value[]} */ (null);
current_dependencies_index = 0; current_dependencies_index = 0;
current_untracked_writes = null; current_untracked_writes = null;
current_reaction = signal; current_reaction = (signal.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? signal : null;
current_skip_reaction = !is_flushing_effect && (signal.f & UNOWNED) !== 0; current_skip_reaction = !is_flushing_effect && (signal.f & UNOWNED) !== 0;
current_untracking = false;
try { try {
let res = /** @type {Function} */ (0, signal.fn)(); let res = /** @type {Function} */ (0, signal.fn)();
@ -429,7 +419,6 @@ export function execute_reaction_fn(signal) {
current_untracked_writes = previous_untracked_writes; current_untracked_writes = previous_untracked_writes;
current_reaction = previous_reaction; current_reaction = previous_reaction;
current_skip_reaction = previous_skip_reaction; current_skip_reaction = previous_skip_reaction;
current_untracking = previous_untracking;
} }
} }
@ -820,11 +809,7 @@ export function get(signal) {
} }
// Register the dependency on the current reaction signal. // Register the dependency on the current reaction signal.
if ( if (current_reaction !== null) {
current_reaction !== null &&
(current_reaction.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 &&
!current_untracking
) {
const unowned = (current_reaction.f & UNOWNED) !== 0; const unowned = (current_reaction.f & UNOWNED) !== 0;
const dependencies = current_reaction.deps; const dependencies = current_reaction.deps;
if ( if (
@ -967,12 +952,12 @@ export function mark_reactions(signal, to_status, force_schedule) {
* @returns {T} * @returns {T}
*/ */
export function untrack(fn) { export function untrack(fn) {
const previous_untracking = current_untracking; const previous_reaction = current_reaction;
try { try {
current_untracking = true; current_reaction = null;
return fn(); return fn();
} finally { } finally {
current_untracking = previous_untracking; current_reaction = previous_reaction;
} }
} }

Loading…
Cancel
Save