chore: improve runtime variable naming (#13275)

better-docs-for-css-injected
Dominic Gannaway 4 days ago committed by GitHub
parent ed7611b163
commit eba64f7cb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,7 +1,7 @@
/** @import { ComponentContext, ComponentContextLegacy } from '#client' */ /** @import { ComponentContext, ComponentContextLegacy } from '#client' */
/** @import { EventDispatcher } from './index.js' */ /** @import { EventDispatcher } from './index.js' */
/** @import { NotFunction } from './internal/types.js' */ /** @import { NotFunction } from './internal/types.js' */
import { current_component_context, flush_sync, untrack } from './internal/client/runtime.js'; import { component_context, flush_sync, untrack } from './internal/client/runtime.js';
import { is_array } from './internal/shared/utils.js'; import { is_array } from './internal/shared/utils.js';
import { user_effect } from './internal/client/index.js'; import { user_effect } from './internal/client/index.js';
import * as e from './internal/client/errors.js'; import * as e from './internal/client/errors.js';
@ -22,12 +22,12 @@ import { lifecycle_outside_component } from './internal/shared/errors.js';
* @returns {void} * @returns {void}
*/ */
export function onMount(fn) { export function onMount(fn) {
if (current_component_context === null) { if (component_context === null) {
lifecycle_outside_component('onMount'); lifecycle_outside_component('onMount');
} }
if (current_component_context.l !== null) { if (component_context.l !== null) {
init_update_callbacks(current_component_context).m.push(fn); init_update_callbacks(component_context).m.push(fn);
} else { } else {
user_effect(() => { user_effect(() => {
const cleanup = untrack(fn); const cleanup = untrack(fn);
@ -47,7 +47,7 @@ export function onMount(fn) {
* @returns {void} * @returns {void}
*/ */
export function onDestroy(fn) { export function onDestroy(fn) {
if (current_component_context === null) { if (component_context === null) {
lifecycle_outside_component('onDestroy'); lifecycle_outside_component('onDestroy');
} }
@ -90,14 +90,14 @@ function create_custom_event(type, detail, { bubbles = false, cancelable = false
* @returns {EventDispatcher<EventMap>} * @returns {EventDispatcher<EventMap>}
*/ */
export function createEventDispatcher() { export function createEventDispatcher() {
const component_context = current_component_context; const active_component_context = component_context;
if (component_context === null) { if (active_component_context === null) {
lifecycle_outside_component('createEventDispatcher'); lifecycle_outside_component('createEventDispatcher');
} }
return (type, detail, options) => { return (type, detail, options) => {
const events = /** @type {Record<string, Function | Function[]>} */ ( const events = /** @type {Record<string, Function | Function[]>} */ (
component_context.s.$$events active_component_context.s.$$events
)?.[/** @type {any} */ (type)]; )?.[/** @type {any} */ (type)];
if (events) { if (events) {
@ -106,7 +106,7 @@ export function createEventDispatcher() {
// in a server (non-DOM) environment? // in a server (non-DOM) environment?
const event = create_custom_event(/** @type {string} */ (type), detail, options); const event = create_custom_event(/** @type {string} */ (type), detail, options);
for (const fn of callbacks) { for (const fn of callbacks) {
fn.call(component_context.x, event); fn.call(active_component_context.x, event);
} }
return !event.defaultPrevented; return !event.defaultPrevented;
} }
@ -130,15 +130,15 @@ export function createEventDispatcher() {
* @returns {void} * @returns {void}
*/ */
export function beforeUpdate(fn) { export function beforeUpdate(fn) {
if (current_component_context === null) { if (component_context === null) {
lifecycle_outside_component('beforeUpdate'); lifecycle_outside_component('beforeUpdate');
} }
if (current_component_context.l === null) { if (component_context.l === null) {
e.lifecycle_legacy_only('beforeUpdate'); e.lifecycle_legacy_only('beforeUpdate');
} }
init_update_callbacks(current_component_context).b.push(fn); init_update_callbacks(component_context).b.push(fn);
} }
/** /**
@ -154,15 +154,15 @@ export function beforeUpdate(fn) {
* @returns {void} * @returns {void}
*/ */
export function afterUpdate(fn) { export function afterUpdate(fn) {
if (current_component_context === null) { if (component_context === null) {
lifecycle_outside_component('afterUpdate'); lifecycle_outside_component('afterUpdate');
} }
if (current_component_context.l === null) { if (component_context.l === null) {
e.lifecycle_legacy_only('afterUpdate'); e.lifecycle_legacy_only('afterUpdate');
} }
init_update_callbacks(current_component_context).a.push(fn); init_update_callbacks(component_context).a.push(fn);
} }
/** /**

@ -1,5 +1,5 @@
import * as e from '../errors.js'; import * as e from '../errors.js';
import { current_component_context } from '../runtime.js'; import { component_context } from '../runtime.js';
import { FILENAME } from '../../../constants.js'; import { FILENAME } from '../../../constants.js';
import { get_component } from './ownership.js'; import { get_component } from './ownership.js';
@ -11,7 +11,7 @@ export function check_target(target) {
} }
export function legacy_api() { export function legacy_api() {
const component = current_component_context?.function; const component = component_context?.function;
/** @param {string} method */ /** @param {string} method */
function error(method) { function error(method) {

@ -1,12 +1,12 @@
/** @import { Effect, Source, TemplateNode } from '#client' */ /** @import { Effect, Source, TemplateNode } from '#client' */
import { is_promise, noop } from '../../../shared/utils.js'; import { is_promise, noop } from '../../../shared/utils.js';
import { import {
current_component_context, component_context,
flush_sync, flush_sync,
is_runes, is_runes,
set_current_component_context, set_component_context,
set_current_effect, set_active_effect,
set_current_reaction, set_active_reaction,
set_dev_current_component_function set_dev_current_component_function
} from '../../runtime.js'; } from '../../runtime.js';
import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js'; import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js';
@ -35,7 +35,7 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
var anchor = node; var anchor = node;
var runes = is_runes(); var runes = is_runes();
var component_context = current_component_context; var active_component_context = component_context;
/** @type {any} */ /** @type {any} */
var component_function = DEV ? component_context?.function : null; var component_function = DEV ? component_context?.function : null;
@ -64,9 +64,9 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
resolved = true; resolved = true;
if (restore) { if (restore) {
set_current_effect(effect); set_active_effect(effect);
set_current_reaction(effect); // TODO do we need both? set_active_reaction(effect); // TODO do we need both?
set_current_component_context(component_context); set_component_context(active_component_context);
if (DEV) set_dev_current_component_function(component_function); if (DEV) set_dev_current_component_function(component_function);
} }
@ -99,9 +99,9 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
if (restore) { if (restore) {
if (DEV) set_dev_current_component_function(null); if (DEV) set_dev_current_component_function(null);
set_current_component_context(null); set_component_context(null);
set_current_reaction(null); set_active_reaction(null);
set_current_effect(null); set_active_effect(null);
// without this, the DOM does not update until two ticks after the promise // without this, the DOM does not update until two ticks after the promise
// resolves, which is unexpected behaviour (and somewhat irksome to test) // resolves, which is unexpected behaviour (and somewhat irksome to test)

@ -35,7 +35,7 @@ import { source, mutable_source, set } from '../../reactivity/sources.js';
import { array_from, is_array } from '../../../shared/utils.js'; import { array_from, is_array } from '../../../shared/utils.js';
import { INERT } from '../../constants.js'; import { INERT } from '../../constants.js';
import { queue_micro_task } from '../task.js'; import { queue_micro_task } from '../task.js';
import { current_effect } from '../../runtime.js'; import { active_effect } from '../../runtime.js';
/** /**
* The row of a keyed each block that is currently updating. We track this * The row of a keyed each block that is currently updating. We track this
@ -426,8 +426,8 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
}); });
} }
/** @type {Effect} */ (current_effect).first = state.first && state.first.e; /** @type {Effect} */ (active_effect).first = state.first && state.first.e;
/** @type {Effect} */ (current_effect).last = prev && prev.e; /** @type {Effect} */ (active_effect).last = prev && prev.e;
} }
/** /**

@ -17,7 +17,7 @@ import {
} from '../../reactivity/effects.js'; } from '../../reactivity/effects.js';
import { set_should_intro } from '../../render.js'; import { set_should_intro } from '../../render.js';
import { current_each_item, set_current_each_item } from './each.js'; import { current_each_item, set_current_each_item } from './each.js';
import { current_component_context, current_effect } from '../../runtime.js'; import { component_context, active_effect } from '../../runtime.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { EFFECT_TRANSPARENT } from '../../constants.js'; import { EFFECT_TRANSPARENT } from '../../constants.js';
import { assign_nodes } from '../template.js'; import { assign_nodes } from '../template.js';
@ -38,7 +38,7 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
hydrate_next(); hydrate_next();
} }
var filename = DEV && location && current_component_context?.function[FILENAME]; var filename = DEV && location && component_context?.function[FILENAME];
/** @type {string | null} */ /** @type {string | null} */
var tag; var tag;
@ -138,7 +138,7 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
} }
// we do this after calling `render_fn` so that child effects don't override `nodes.end` // we do this after calling `render_fn` so that child effects don't override `nodes.end`
/** @type {Effect} */ (current_effect).nodes_end = element; /** @type {Effect} */ (active_effect).nodes_end = element;
anchor.before(element); anchor.before(element);
}); });

@ -1,7 +1,7 @@
/** @import { AnimateFn, Animation, AnimationConfig, EachItem, Effect, TransitionFn, TransitionManager } from '#client' */ /** @import { AnimateFn, Animation, AnimationConfig, EachItem, Effect, TransitionFn, TransitionManager } from '#client' */
import { noop, is_function } from '../../../shared/utils.js'; import { noop, is_function } from '../../../shared/utils.js';
import { effect } from '../../reactivity/effects.js'; import { effect } from '../../reactivity/effects.js';
import { current_effect, untrack } from '../../runtime.js'; import { active_effect, untrack } from '../../runtime.js';
import { loop } from '../../loop.js'; import { loop } from '../../loop.js';
import { should_intro } from '../../render.js'; import { should_intro } from '../../render.js';
import { current_each_item } from '../blocks/each.js'; import { current_each_item } from '../blocks/each.js';
@ -243,7 +243,7 @@ export function transition(flags, element, get_fn, get_params) {
} }
}; };
var e = /** @type {Effect} */ (current_effect); var e = /** @type {Effect} */ (active_effect);
(e.transitions ??= []).push(transition); (e.transitions ??= []).push(transition);

@ -1,13 +1,13 @@
/** @import { ComponentContextLegacy } from '#client' */ /** @import { ComponentContextLegacy } from '#client' */
import { run, run_all } from '../../../shared/utils.js'; import { run, run_all } from '../../../shared/utils.js';
import { user_pre_effect, user_effect } from '../../reactivity/effects.js'; import { user_pre_effect, user_effect } from '../../reactivity/effects.js';
import { current_component_context, deep_read_state, get, untrack } from '../../runtime.js'; import { component_context, deep_read_state, get, untrack } from '../../runtime.js';
/** /**
* Legacy-mode only: Call `onMount` callbacks and set up `beforeUpdate`/`afterUpdate` effects * Legacy-mode only: Call `onMount` callbacks and set up `beforeUpdate`/`afterUpdate` effects
*/ */
export function init() { export function init() {
const context = /** @type {ComponentContextLegacy} */ (current_component_context); const context = /** @type {ComponentContextLegacy} */ (component_context);
const callbacks = context.l.u; const callbacks = context.l.u;
if (!callbacks) return; if (!callbacks) return;

@ -2,7 +2,7 @@
import { hydrate_next, hydrate_node, hydrating, set_hydrate_node } from './hydration.js'; import { hydrate_next, hydrate_node, hydrating, set_hydrate_node } from './hydration.js';
import { create_text, get_first_child } from './operations.js'; import { create_text, get_first_child } from './operations.js';
import { create_fragment_from_html } from './reconciler.js'; import { create_fragment_from_html } from './reconciler.js';
import { current_effect } from '../runtime.js'; import { active_effect } from '../runtime.js';
import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../constants.js'; import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../constants.js';
import { queue_micro_task } from './task.js'; import { queue_micro_task } from './task.js';
@ -11,7 +11,7 @@ import { queue_micro_task } from './task.js';
* @param {TemplateNode | null} end * @param {TemplateNode | null} end
*/ */
export function assign_nodes(start, end) { export function assign_nodes(start, end) {
var effect = /** @type {Effect} */ (current_effect); var effect = /** @type {Effect} */ (active_effect);
if (effect.nodes_start === null) { if (effect.nodes_start === null) {
effect.nodes_start = start; effect.nodes_start = start;
effect.nodes_end = end; effect.nodes_end = end;
@ -192,7 +192,7 @@ function run_scripts(node) {
/** @type {HTMLElement} */ (node).tagName === 'SCRIPT' /** @type {HTMLElement} */ (node).tagName === 'SCRIPT'
? [/** @type {HTMLScriptElement} */ (node)] ? [/** @type {HTMLScriptElement} */ (node)]
: node.querySelectorAll('script'); : node.querySelectorAll('script');
const effect = /** @type {Effect} */ (current_effect); const effect = /** @type {Effect} */ (active_effect);
for (const script of scripts) { for (const script of scripts) {
const clone = document.createElement('script'); const clone = document.createElement('script');
@ -274,7 +274,7 @@ export function comment() {
*/ */
export function append(anchor, dom) { export function append(anchor, dom) {
if (hydrating) { if (hydrating) {
/** @type {Effect} */ (current_effect).nodes_end = hydrate_node; /** @type {Effect} */ (active_effect).nodes_end = hydrate_node;
hydrate_next(); hydrate_next();
return; return;
} }

@ -1,6 +1,6 @@
/** @import { ProxyMetadata, ProxyStateObject, Source } from '#client' */ /** @import { ProxyMetadata, ProxyStateObject, Source } from '#client' */
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { get, current_component_context, untrack, current_effect } from './runtime.js'; import { get, component_context, active_effect } from './runtime.js';
import { import {
array_prototype, array_prototype,
get_descriptor, get_descriptor,
@ -62,8 +62,8 @@ export function proxy(value, parent = null, prev) {
} else { } else {
metadata.owners = metadata.owners =
parent === null parent === null
? current_component_context !== null ? component_context !== null
? new Set([current_component_context.function]) ? new Set([component_context.function])
: null : null
: new Set(); : new Set();
} }
@ -190,7 +190,7 @@ export function proxy(value, parent = null, prev) {
if ( if (
s !== undefined || s !== undefined ||
(current_effect !== null && (!has || get_descriptor(target, prop)?.writable)) (active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
) { ) {
if (s === undefined) { if (s === undefined) {
s = source(has ? proxy(target[prop], metadata) : UNINITIALIZED); s = source(has ? proxy(target[prop], metadata) : UNINITIALIZED);

@ -2,11 +2,11 @@
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { CLEAN, DERIVED, DESTROYED, DIRTY, MAYBE_DIRTY, UNOWNED } from '../constants.js'; import { CLEAN, DERIVED, DESTROYED, DIRTY, MAYBE_DIRTY, UNOWNED } from '../constants.js';
import { import {
current_reaction, active_reaction,
current_effect, active_effect,
remove_reactions, remove_reactions,
set_signal_status, set_signal_status,
current_skip_reaction, skip_reaction,
update_reaction, update_reaction,
increment_version increment_version
} from '../runtime.js'; } from '../runtime.js';
@ -23,7 +23,7 @@ import { inspect_effects, set_inspect_effects } from './sources.js';
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function derived(fn) { export function derived(fn) {
let flags = DERIVED | DIRTY; let flags = DERIVED | DIRTY;
if (current_effect === null) flags |= UNOWNED; if (active_effect === null) flags |= UNOWNED;
/** @type {Derived<V>} */ /** @type {Derived<V>} */
const signal = { const signal = {
@ -37,8 +37,8 @@ export function derived(fn) {
version: 0 version: 0
}; };
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) { if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
var derived = /** @type {Derived} */ (current_reaction); var derived = /** @type {Derived} */ (active_reaction);
(derived.children ??= []).push(signal); (derived.children ??= []).push(signal);
} }
@ -114,9 +114,7 @@ export function update_derived(derived) {
} }
var status = var status =
(current_skip_reaction || (derived.f & UNOWNED) !== 0) && derived.deps !== null (skip_reaction || (derived.f & UNOWNED) !== 0) && derived.deps !== null ? MAYBE_DIRTY : CLEAN;
? MAYBE_DIRTY
: CLEAN;
set_signal_status(derived, status); set_signal_status(derived, status);

@ -1,9 +1,9 @@
/** @import { ComponentContext, ComponentContextLegacy, Derived, Effect, Reaction, TemplateNode, TransitionManager } from '#client' */ /** @import { ComponentContext, ComponentContextLegacy, Derived, Effect, Reaction, TemplateNode, TransitionManager } from '#client' */
import { import {
check_dirtiness, check_dirtiness,
current_component_context, component_context,
current_effect, active_effect,
current_reaction, active_reaction,
destroy_effect_children, destroy_effect_children,
dev_current_component_function, dev_current_component_function,
update_effect, update_effect,
@ -12,7 +12,7 @@ import {
is_flushing_effect, is_flushing_effect,
remove_reactions, remove_reactions,
schedule_effect, schedule_effect,
set_current_reaction, set_active_reaction,
set_is_destroying_effect, set_is_destroying_effect,
set_is_flushing_effect, set_is_flushing_effect,
set_signal_status, set_signal_status,
@ -46,11 +46,11 @@ import { get_next_sibling } from '../dom/operations.js';
* @param {'$effect' | '$effect.pre' | '$inspect'} rune * @param {'$effect' | '$effect.pre' | '$inspect'} rune
*/ */
export function validate_effect(rune) { export function validate_effect(rune) {
if (current_effect === null && current_reaction === null) { if (active_effect === null && active_reaction === null) {
e.effect_orphan(rune); e.effect_orphan(rune);
} }
if (current_reaction !== null && (current_reaction.f & UNOWNED) !== 0) { if (active_reaction !== null && (active_reaction.f & UNOWNED) !== 0) {
e.effect_in_unowned_derived(); e.effect_in_unowned_derived();
} }
@ -83,7 +83,7 @@ function push_effect(effect, parent_effect) {
*/ */
function create_effect(type, fn, sync, push = true) { function create_effect(type, fn, sync, push = true) {
var is_root = (type & ROOT_EFFECT) !== 0; var is_root = (type & ROOT_EFFECT) !== 0;
var parent_effect = current_effect; var parent_effect = active_effect;
if (DEV) { if (DEV) {
// Ensure the parent is never an inspect effect // Ensure the parent is never an inspect effect
@ -94,7 +94,7 @@ function create_effect(type, fn, sync, push = true) {
/** @type {Effect} */ /** @type {Effect} */
var effect = { var effect = {
ctx: current_component_context, ctx: component_context,
deps: null, deps: null,
nodes_start: null, nodes_start: null,
nodes_end: null, nodes_end: null,
@ -146,8 +146,8 @@ function create_effect(type, fn, sync, push = true) {
} }
// if we're in a derived, add the effect there too // if we're in a derived, add the effect there too
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) { if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
var derived = /** @type {Derived} */ (current_reaction); var derived = /** @type {Derived} */ (active_reaction);
(derived.children ??= []).push(effect); (derived.children ??= []).push(effect);
} }
} }
@ -160,11 +160,11 @@ function create_effect(type, fn, sync, push = true) {
* @returns {boolean} * @returns {boolean}
*/ */
export function effect_tracking() { export function effect_tracking() {
if (current_reaction === null) { if (active_reaction === null) {
return false; return false;
} }
return (current_reaction.f & UNOWNED) === 0; return (active_reaction.f & UNOWNED) === 0;
} }
/** /**
@ -187,11 +187,11 @@ export function user_effect(fn) {
// Non-nested `$effect(...)` in a component should be deferred // Non-nested `$effect(...)` in a component should be deferred
// until the component is mounted // until the component is mounted
var defer = var defer =
current_effect !== null && active_effect !== null &&
(current_effect.f & RENDER_EFFECT) !== 0 && (active_effect.f & RENDER_EFFECT) !== 0 &&
// TODO do we actually need this? removing them changes nothing // TODO do we actually need this? removing them changes nothing
current_component_context !== null && component_context !== null &&
!current_component_context.m; !component_context.m;
if (DEV) { if (DEV) {
define_property(fn, 'name', { define_property(fn, 'name', {
@ -200,7 +200,7 @@ export function user_effect(fn) {
} }
if (defer) { if (defer) {
var context = /** @type {ComponentContext} */ (current_component_context); var context = /** @type {ComponentContext} */ (component_context);
(context.e ??= []).push(fn); (context.e ??= []).push(fn);
} else { } else {
var signal = effect(fn); var signal = effect(fn);
@ -254,7 +254,7 @@ export function effect(fn) {
* @param {() => void | (() => void)} fn * @param {() => void | (() => void)} fn
*/ */
export function legacy_pre_effect(deps, fn) { export function legacy_pre_effect(deps, fn) {
var context = /** @type {ComponentContextLegacy} */ (current_component_context); var context = /** @type {ComponentContextLegacy} */ (component_context);
/** @type {{ effect: null | Effect, ran: boolean }} */ /** @type {{ effect: null | Effect, ran: boolean }} */
var token = { effect: null, ran: false }; var token = { effect: null, ran: false };
@ -274,7 +274,7 @@ export function legacy_pre_effect(deps, fn) {
} }
export function legacy_pre_effect_reset() { export function legacy_pre_effect_reset() {
var context = /** @type {ComponentContextLegacy} */ (current_component_context); var context = /** @type {ComponentContextLegacy} */ (component_context);
render_effect(() => { render_effect(() => {
if (!get(context.l.r2)) return; if (!get(context.l.r2)) return;
@ -344,14 +344,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_reaction = current_reaction; const previous_reaction = active_reaction;
set_is_destroying_effect(true); set_is_destroying_effect(true);
set_current_reaction(null); set_active_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_current_reaction(previous_reaction); set_active_reaction(previous_reaction);
} }
} }
} }

@ -1,15 +1,15 @@
/** @import { Derived, Effect, Reaction, Source, Value } from '#client' */ /** @import { Derived, Effect, Reaction, Source, Value } from '#client' */
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { import {
current_component_context, component_context,
current_reaction, active_reaction,
new_deps, new_deps,
current_effect, active_effect,
current_untracked_writes, untracked_writes,
get, get,
is_runes, is_runes,
schedule_effect, schedule_effect,
set_current_untracked_writes, set_untracked_writes,
set_signal_status, set_signal_status,
untrack, untrack,
increment_version, increment_version,
@ -76,8 +76,8 @@ export function mutable_source(initial_value) {
// bind the signal to the component context, in case we need to // bind the signal to the component context, in case we need to
// track updates to trigger beforeUpdate/afterUpdate callbacks // track updates to trigger beforeUpdate/afterUpdate callbacks
if (current_component_context !== null && current_component_context.l !== null) { if (component_context !== null && component_context.l !== null) {
(current_component_context.l.s ??= []).push(s); (component_context.l.s ??= []).push(s);
} }
return s; return s;
@ -98,7 +98,7 @@ export function mutable_state(v) {
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
function push_derived_source(source) { function push_derived_source(source) {
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) { if (active_reaction !== null && (active_reaction.f & DERIVED) !== 0) {
if (derived_sources === null) { if (derived_sources === null) {
set_derived_sources([source]); set_derived_sources([source]);
} else { } else {
@ -130,9 +130,9 @@ export function mutate(source, value) {
*/ */
export function set(source, value) { export function set(source, value) {
if ( if (
current_reaction !== null && active_reaction !== null &&
is_runes() && is_runes() &&
(current_reaction.f & DERIVED) !== 0 && (active_reaction.f & DERIVED) !== 0 &&
// If the source was created locally within the current derived, then // If the source was created locally within the current derived, then
// we allow the mutation. // we allow the mutation.
(derived_sources === null || !derived_sources.includes(source)) (derived_sources === null || !derived_sources.includes(source))
@ -153,18 +153,18 @@ export function set(source, value) {
// scheduled. i.e: `$effect(() => x++)` // scheduled. i.e: `$effect(() => x++)`
if ( if (
is_runes() && is_runes() &&
current_effect !== null && active_effect !== null &&
(current_effect.f & CLEAN) !== 0 && (active_effect.f & CLEAN) !== 0 &&
(current_effect.f & BRANCH_EFFECT) === 0 (active_effect.f & BRANCH_EFFECT) === 0
) { ) {
if (new_deps !== null && new_deps.includes(source)) { if (new_deps !== null && new_deps.includes(source)) {
set_signal_status(current_effect, DIRTY); set_signal_status(active_effect, DIRTY);
schedule_effect(current_effect); schedule_effect(active_effect);
} else { } else {
if (current_untracked_writes === null) { if (untracked_writes === null) {
set_current_untracked_writes([source]); set_untracked_writes([source]);
} else { } else {
current_untracked_writes.push(source); untracked_writes.push(source);
} }
} }
} }
@ -214,7 +214,7 @@ function mark_reactions(signal, status) {
if ((flags & DIRTY) !== 0) continue; if ((flags & DIRTY) !== 0) continue;
// In legacy mode, skip the current effect to prevent infinite loops // In legacy mode, skip the current effect to prevent infinite loops
if (!runes && reaction === current_effect) continue; if (!runes && reaction === active_effect) continue;
// Inspect effects need to run immediately, so that the stack trace makes sense // Inspect effects need to run immediately, so that the stack trace makes sense
if (DEV && (flags & INSPECT_EFFECT) !== 0) { if (DEV && (flags & INSPECT_EFFECT) !== 0) {

@ -9,7 +9,7 @@ import {
init_operations init_operations
} from './dom/operations.js'; } from './dom/operations.js';
import { HYDRATION_END, HYDRATION_ERROR, HYDRATION_START } from '../../constants.js'; import { HYDRATION_END, HYDRATION_ERROR, HYDRATION_START } from '../../constants.js';
import { push, pop, current_component_context, current_effect } from './runtime.js'; import { push, pop, component_context, active_effect } from './runtime.js';
import { effect_root, branch } from './reactivity/effects.js'; import { effect_root, branch } from './reactivity/effects.js';
import { import {
hydrate_next, hydrate_next,
@ -228,7 +228,7 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
branch(() => { branch(() => {
if (context) { if (context) {
push({}); push({});
var ctx = /** @type {ComponentContext} */ (current_component_context); var ctx = /** @type {ComponentContext} */ (component_context);
ctx.c = context; ctx.c = context;
} }
@ -247,7 +247,7 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
should_intro = true; should_intro = true;
if (hydrating) { if (hydrating) {
/** @type {Effect} */ (current_effect).nodes_end = hydrate_node; /** @type {Effect} */ (active_effect).nodes_end = hydrate_node;
} }
if (context) { if (context) {

@ -39,7 +39,7 @@ const FLUSH_SYNC = 1;
/** @param {WeakSet<Error>} value */ /** @param {WeakSet<Error>} value */
const handled_errors = new WeakSet(); const handled_errors = new WeakSet();
// Used for controlling the flush of effects. // Used for controlling the flush of effects.
let current_scheduler_mode = FLUSH_MICROTASK; let scheduler_mode = FLUSH_MICROTASK;
// Used for handling scheduling // Used for handling scheduling
let is_micro_task_queued = false; let is_micro_task_queued = false;
@ -59,7 +59,7 @@ export function set_is_destroying_effect(value) {
// Handle effect queues // Handle effect queues
/** @type {Effect[]} */ /** @type {Effect[]} */
let current_queued_root_effects = []; let queued_root_effects = [];
let flush_count = 0; let flush_count = 0;
/** @type {Effect[]} Stack of effects, dev only */ /** @type {Effect[]} Stack of effects, dev only */
@ -67,19 +67,19 @@ let dev_effect_stack = [];
// Handle signal reactivity tree dependencies and reactions // Handle signal reactivity tree dependencies and reactions
/** @type {null | Reaction} */ /** @type {null | Reaction} */
export let current_reaction = null; export let active_reaction = null;
/** @param {null | Reaction} reaction */ /** @param {null | Reaction} reaction */
export function set_current_reaction(reaction) { export function set_active_reaction(reaction) {
current_reaction = reaction; active_reaction = reaction;
} }
/** @type {null | Effect} */ /** @type {null | Effect} */
export let current_effect = null; export let active_effect = null;
/** @param {null | Effect} effect */ /** @param {null | Effect} effect */
export function set_current_effect(effect) { export function set_active_effect(effect) {
current_effect = effect; active_effect = effect;
} }
/** /**
@ -111,11 +111,11 @@ let skipped_deps = 0;
* so that the dependency can be added to the effect later on if it then reads it * so that the dependency can be added to the effect later on if it then reads it
* @type {null | Source[]} * @type {null | Source[]}
*/ */
export let current_untracked_writes = null; export let untracked_writes = null;
/** @param {null | Source[]} value */ /** @param {null | Source[]} value */
export function set_current_untracked_writes(value) { export function set_untracked_writes(value) {
current_untracked_writes = value; untracked_writes = value;
} }
/** @type {number} Used by sources and deriveds for handling updates to unowned deriveds */ /** @type {number} Used by sources and deriveds for handling updates to unowned deriveds */
@ -123,18 +123,18 @@ 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,
// to prevent memory leaks, we skip adding the reaction. // to prevent memory leaks, we skip adding the reaction.
export let current_skip_reaction = false; export let skip_reaction = false;
// Handle collecting all signals which are read during a specific time frame // Handle collecting all signals which are read during a specific time frame
export let is_signals_recorded = false; export let is_signals_recorded = false;
let captured_signals = new Set(); let captured_signals = new Set();
// Handling runtime component context // Handling runtime component context
/** @type {ComponentContext | null} */ /** @type {ComponentContext | null} */
export let current_component_context = null; export let component_context = null;
/** @param {ComponentContext | null} context */ /** @param {ComponentContext | null} context */
export function set_current_component_context(context) { export function set_component_context(context) {
current_component_context = context; component_context = context;
} }
/** /**
@ -160,7 +160,7 @@ export function increment_version() {
/** @returns {boolean} */ /** @returns {boolean} */
export function is_runes() { export function is_runes() {
return current_component_context !== null && current_component_context.l === null; return component_context !== null && component_context.l === null;
} }
/** /**
@ -198,13 +198,13 @@ export function check_dirtiness(reaction) {
update_derived(/** @type {Derived} */ (dependency)); update_derived(/** @type {Derived} */ (dependency));
} }
// If we are working with an unowned signal as part of an effect (due to !current_skip_reaction) // If we are working with an unowned signal as part of an effect (due to !skip_reaction)
// and the version hasn't changed, we still need to check that this reaction // and the version hasn't changed, we still need to check that this reaction
// is linked to the dependency source otherwise future updates will not be caught. // is linked to the dependency source otherwise future updates will not be caught.
if ( if (
is_unowned && is_unowned &&
current_effect !== null && active_effect !== null &&
!current_skip_reaction && !skip_reaction &&
!dependency?.reactions?.includes(reaction) !dependency?.reactions?.includes(reaction)
) { ) {
(dependency.reactions ??= []).push(reaction); (dependency.reactions ??= []).push(reaction);
@ -296,16 +296,16 @@ function handle_error(error, effect, component_context) {
export function update_reaction(reaction) { export function update_reaction(reaction) {
var previous_deps = new_deps; var previous_deps = new_deps;
var previous_skipped_deps = skipped_deps; var previous_skipped_deps = skipped_deps;
var previous_untracked_writes = current_untracked_writes; var previous_untracked_writes = untracked_writes;
var previous_reaction = current_reaction; var previous_reaction = active_reaction;
var previous_skip_reaction = current_skip_reaction; var previous_skip_reaction = skip_reaction;
var prev_derived_sources = derived_sources; var prev_derived_sources = derived_sources;
new_deps = /** @type {null | Value[]} */ (null); new_deps = /** @type {null | Value[]} */ (null);
skipped_deps = 0; skipped_deps = 0;
current_untracked_writes = null; untracked_writes = null;
current_reaction = (reaction.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null; active_reaction = (reaction.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null;
current_skip_reaction = !is_flushing_effect && (reaction.f & UNOWNED) !== 0; skip_reaction = !is_flushing_effect && (reaction.f & UNOWNED) !== 0;
derived_sources = null; derived_sources = null;
try { try {
@ -326,7 +326,7 @@ export function update_reaction(reaction) {
reaction.deps = deps = new_deps; reaction.deps = deps = new_deps;
} }
if (!current_skip_reaction) { if (!skip_reaction) {
for (i = skipped_deps; i < deps.length; i++) { for (i = skipped_deps; i < deps.length; i++) {
(deps[i].reactions ??= []).push(reaction); (deps[i].reactions ??= []).push(reaction);
} }
@ -340,9 +340,9 @@ export function update_reaction(reaction) {
} finally { } finally {
new_deps = previous_deps; new_deps = previous_deps;
skipped_deps = previous_skipped_deps; skipped_deps = previous_skipped_deps;
current_untracked_writes = previous_untracked_writes; untracked_writes = previous_untracked_writes;
current_reaction = previous_reaction; active_reaction = previous_reaction;
current_skip_reaction = previous_skip_reaction; skip_reaction = previous_skip_reaction;
derived_sources = prev_derived_sources; derived_sources = prev_derived_sources;
} }
} }
@ -424,13 +424,11 @@ export function update_effect(effect) {
set_signal_status(effect, CLEAN); set_signal_status(effect, CLEAN);
var component_context = effect.ctx; var previous_effect = active_effect;
var previous_component_context = component_context;
var previous_effect = current_effect; active_effect = effect;
var previous_component_context = current_component_context; component_context = effect.ctx;
current_effect = effect;
current_component_context = component_context;
if (DEV) { if (DEV) {
var previous_component_fn = dev_current_component_function; var previous_component_fn = dev_current_component_function;
@ -451,10 +449,10 @@ export function update_effect(effect) {
dev_effect_stack.push(effect); dev_effect_stack.push(effect);
} }
} catch (error) { } catch (error) {
handle_error(/** @type {Error} */ (error), effect, current_component_context); handle_error(/** @type {Error} */ (error), effect, previous_component_context);
} finally { } finally {
current_effect = previous_effect; active_effect = previous_effect;
current_component_context = previous_component_context; component_context = previous_component_context;
if (DEV) { if (DEV) {
dev_current_component_function = previous_component_fn; dev_current_component_function = previous_component_fn;
@ -559,8 +557,8 @@ function process_deferred() {
if (flush_count > 1001) { if (flush_count > 1001) {
return; return;
} }
const previous_queued_root_effects = current_queued_root_effects; const previous_queued_root_effects = queued_root_effects;
current_queued_root_effects = []; queued_root_effects = [];
flush_queued_root_effects(previous_queued_root_effects); flush_queued_root_effects(previous_queued_root_effects);
if (!is_micro_task_queued) { if (!is_micro_task_queued) {
flush_count = 0; flush_count = 0;
@ -575,7 +573,7 @@ function process_deferred() {
* @returns {void} * @returns {void}
*/ */
export function schedule_effect(signal) { export function schedule_effect(signal) {
if (current_scheduler_mode === FLUSH_MICROTASK) { if (scheduler_mode === FLUSH_MICROTASK) {
if (!is_micro_task_queued) { if (!is_micro_task_queued) {
is_micro_task_queued = true; is_micro_task_queued = true;
queueMicrotask(process_deferred); queueMicrotask(process_deferred);
@ -594,7 +592,7 @@ export function schedule_effect(signal) {
} }
} }
current_queued_root_effects.push(effect); queued_root_effects.push(effect);
} }
/** /**
@ -685,8 +683,8 @@ function process_effects(effect, collected_effects) {
* @returns {any} * @returns {any}
*/ */
export function flush_sync(fn) { export function flush_sync(fn) {
var previous_scheduler_mode = current_scheduler_mode; var previous_scheduler_mode = scheduler_mode;
var previous_queued_root_effects = current_queued_root_effects; var previous_queued_root_effects = queued_root_effects;
try { try {
infinite_loop_guard(); infinite_loop_guard();
@ -694,8 +692,8 @@ export function flush_sync(fn) {
/** @type {Effect[]} */ /** @type {Effect[]} */
const root_effects = []; const root_effects = [];
current_scheduler_mode = FLUSH_SYNC; scheduler_mode = FLUSH_SYNC;
current_queued_root_effects = root_effects; queued_root_effects = root_effects;
is_micro_task_queued = false; is_micro_task_queued = false;
flush_queued_root_effects(previous_queued_root_effects); flush_queued_root_effects(previous_queued_root_effects);
@ -703,7 +701,7 @@ export function flush_sync(fn) {
var result = fn?.(); var result = fn?.();
flush_tasks(); flush_tasks();
if (current_queued_root_effects.length > 0 || root_effects.length > 0) { if (queued_root_effects.length > 0 || root_effects.length > 0) {
flush_sync(); flush_sync();
} }
@ -714,8 +712,8 @@ export function flush_sync(fn) {
return result; return result;
} finally { } finally {
current_scheduler_mode = previous_scheduler_mode; scheduler_mode = previous_scheduler_mode;
current_queued_root_effects = previous_queued_root_effects; queued_root_effects = previous_queued_root_effects;
} }
} }
@ -747,11 +745,11 @@ export function get(signal) {
} }
// Register the dependency on the current reaction signal. // Register the dependency on the current reaction signal.
if (current_reaction !== null) { if (active_reaction !== null) {
if (derived_sources !== null && derived_sources.includes(signal)) { if (derived_sources !== null && derived_sources.includes(signal)) {
e.state_unsafe_local_read(); e.state_unsafe_local_read();
} }
var deps = current_reaction.deps; var deps = active_reaction.deps;
// If the signal is accessing the same dependencies in the same // If the signal is accessing the same dependencies in the same
// order as it did last time, increment `skipped_deps` // order as it did last time, increment `skipped_deps`
@ -765,14 +763,14 @@ export function get(signal) {
} }
if ( if (
current_untracked_writes !== null && untracked_writes !== null &&
current_effect !== null && active_effect !== null &&
(current_effect.f & CLEAN) !== 0 && (active_effect.f & CLEAN) !== 0 &&
(current_effect.f & BRANCH_EFFECT) === 0 && (active_effect.f & BRANCH_EFFECT) === 0 &&
current_untracked_writes.includes(signal) untracked_writes.includes(signal)
) { ) {
set_signal_status(current_effect, DIRTY); set_signal_status(active_effect, DIRTY);
schedule_effect(current_effect); schedule_effect(active_effect);
} }
} }
@ -843,12 +841,12 @@ export function invalidate_inner_signals(fn) {
* @returns {T} * @returns {T}
*/ */
export function untrack(fn) { export function untrack(fn) {
const previous_reaction = current_reaction; const previous_reaction = active_reaction;
try { try {
current_reaction = null; active_reaction = null;
return fn(); return fn();
} finally { } finally {
current_reaction = previous_reaction; active_reaction = previous_reaction;
} }
} }
@ -877,7 +875,7 @@ export function getContext(key) {
const result = /** @type {T} */ (context_map.get(key)); const result = /** @type {T} */ (context_map.get(key));
if (DEV) { if (DEV) {
const fn = /** @type {ComponentContext} */ (current_component_context).function; const fn = /** @type {ComponentContext} */ (component_context).function;
if (fn) { if (fn) {
add_owner(result, fn, true); add_owner(result, fn, true);
} }
@ -931,7 +929,7 @@ export function getAllContexts() {
const context_map = get_or_init_context_map('getAllContexts'); const context_map = get_or_init_context_map('getAllContexts');
if (DEV) { if (DEV) {
const fn = current_component_context?.function; const fn = component_context?.function;
if (fn) { if (fn) {
for (const value of context_map.values()) { for (const value of context_map.values()) {
add_owner(value, fn, true); add_owner(value, fn, true);
@ -947,13 +945,11 @@ export function getAllContexts() {
* @returns {Map<unknown, unknown>} * @returns {Map<unknown, unknown>}
*/ */
function get_or_init_context_map(name) { function get_or_init_context_map(name) {
if (current_component_context === null) { if (component_context === null) {
lifecycle_outside_component(name); lifecycle_outside_component(name);
} }
return (current_component_context.c ??= new Map( return (component_context.c ??= new Map(get_parent_context(component_context) || undefined));
get_parent_context(current_component_context) || undefined
));
} }
/** /**
@ -1017,8 +1013,8 @@ export function exclude_from_object(obj, keys) {
* @returns {void} * @returns {void}
*/ */
export function push(props, runes = false, fn) { export function push(props, runes = false, fn) {
current_component_context = { component_context = {
p: current_component_context, p: component_context,
c: null, c: null,
e: null, e: null,
m: false, m: false,
@ -1028,7 +1024,7 @@ export function push(props, runes = false, fn) {
}; };
if (!runes) { if (!runes) {
current_component_context.l = { component_context.l = {
s: null, s: null,
u: null, u: null,
r1: [], r1: [],
@ -1038,7 +1034,7 @@ export function push(props, runes = false, fn) {
if (DEV) { if (DEV) {
// component function // component function
current_component_context.function = fn; component_context.function = fn;
dev_current_component_function = fn; dev_current_component_function = fn;
} }
} }
@ -1049,7 +1045,7 @@ export function push(props, runes = false, fn) {
* @returns {T} * @returns {T}
*/ */
export function pop(component) { export function pop(component) {
const context_stack_item = current_component_context; const context_stack_item = component_context;
if (context_stack_item !== null) { if (context_stack_item !== null) {
if (component !== undefined) { if (component !== undefined) {
context_stack_item.x = component; context_stack_item.x = component;
@ -1061,7 +1057,7 @@ export function pop(component) {
effect(effects[i]); effect(effects[i]);
} }
} }
current_component_context = context_stack_item.p; component_context = context_stack_item.p;
if (DEV) { if (DEV) {
dev_current_component_function = context_stack_item.p?.function ?? null; dev_current_component_function = context_stack_item.p?.function ?? null;
} }

Loading…
Cancel
Save