rename inspect effects to eager effects, run them in prod

pull/17004/head
Rich Harris 4 weeks ago
parent 53cc91dd0c
commit 8285fc6c4f

@ -16,7 +16,7 @@ export const DESTROYED = 1 << 14;
export const EFFECT_RAN = 1 << 15; export const EFFECT_RAN = 1 << 15;
/** 'Transparent' effects do not create a transition boundary */ /** 'Transparent' effects do not create a transition boundary */
export const EFFECT_TRANSPARENT = 1 << 16; export const EFFECT_TRANSPARENT = 1 << 16;
export const INSPECT_EFFECT = 1 << 17; export const EAGER_EFFECT = 1 << 17;
export const HEAD_EFFECT = 1 << 18; export const HEAD_EFFECT = 1 << 18;
export const EFFECT_PRESERVED = 1 << 19; export const EFFECT_PRESERVED = 1 << 19;
export const USER_EFFECT = 1 << 20; export const USER_EFFECT = 1 << 20;

@ -1,6 +1,6 @@
import { UNINITIALIZED } from '../../../constants.js'; import { UNINITIALIZED } from '../../../constants.js';
import { snapshot } from '../../shared/clone.js'; import { snapshot } from '../../shared/clone.js';
import { inspect_effect, render_effect, validate_effect } from '../reactivity/effects.js'; import { eager_effect, render_effect, validate_effect } from '../reactivity/effects.js';
import { untrack } from '../runtime.js'; import { untrack } from '../runtime.js';
import { get_stack } from './tracing.js'; import { get_stack } from './tracing.js';
@ -19,7 +19,7 @@ export function inspect(get_value, inspector, show_stack = false) {
// stack traces. As a consequence, reading the value might result // stack traces. As a consequence, reading the value might result
// in an error (an `$inspect(object.property)` will run before the // in an error (an `$inspect(object.property)` will run before the
// `{#if object}...{/if}` that contains it) // `{#if object}...{/if}` that contains it)
inspect_effect(() => { eager_effect(() => {
try { try {
var value = get_value(); var value = get_value();
} catch (e) { } catch (e) {

@ -19,8 +19,8 @@ import {
state as source, state as source,
set, set,
increment, increment,
flush_inspect_effects, flush_eager_effects,
set_inspect_effects_deferred set_eager_effects_deferred
} from './reactivity/sources.js'; } from './reactivity/sources.js';
import { PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants'; import { PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
import { UNINITIALIZED } from '../../constants.js'; import { UNINITIALIZED } from '../../constants.js';
@ -421,9 +421,9 @@ function inspectable_array(array) {
* @param {any[]} args * @param {any[]} args
*/ */
return function (...args) { return function (...args) {
set_inspect_effects_deferred(); set_eager_effects_deferred();
var result = value.apply(this, args); var result = value.apply(this, args);
flush_inspect_effects(); flush_eager_effects();
return result; return result;
}; };
} }

@ -13,7 +13,7 @@ import {
MAYBE_DIRTY, MAYBE_DIRTY,
DERIVED, DERIVED,
BOUNDARY_EFFECT, BOUNDARY_EFFECT,
INSPECT_EFFECT EAGER_EFFECT
} from '#client/constants'; } from '#client/constants';
import { async_mode_flag } from '../../flags/index.js'; import { async_mode_flag } from '../../flags/index.js';
import { deferred, define_property } from '../../shared/utils.js'; import { deferred, define_property } from '../../shared/utils.js';
@ -33,14 +33,14 @@ import { flush_tasks, queue_micro_task } from '../dom/task.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { invoke_error_boundary } from '../error-handling.js'; import { invoke_error_boundary } from '../error-handling.js';
import { import {
flush_inspect_effects, flush_eager_effects,
inspect_effects, eager_effects,
old_values, old_values,
set_inspect_effects, set_eager_effects,
source, source,
update update
} from './sources.js'; } from './sources.js';
import { inspect_effect, unlink_effect } from './effects.js'; import { eager_effect, unlink_effect } from './effects.js';
/** /**
* @typedef {{ * @typedef {{
@ -745,14 +745,14 @@ function mark_effects(value, sources, marked, checked) {
* @param {Value} value * @param {Value} value
* @param {Set<Effect>} effects * @param {Set<Effect>} effects
*/ */
function mark_inspect_effects(value, effects) { function mark_eager_effects(value, effects) {
if (value.reactions !== null) { if (value.reactions !== null) {
for (const reaction of value.reactions) { for (const reaction of value.reactions) {
const flags = reaction.f; const flags = reaction.f;
if ((flags & DERIVED) !== 0) { if ((flags & DERIVED) !== 0) {
mark_inspect_effects(/** @type {Derived} */ (reaction), effects); mark_eager_effects(/** @type {Derived} */ (reaction), effects);
} else if ((flags & INSPECT_EFFECT) !== 0) { } else if ((flags & EAGER_EFFECT) !== 0) {
set_signal_status(reaction, DIRTY); set_signal_status(reaction, DIRTY);
effects.add(/** @type {Effect} */ (reaction)); effects.add(/** @type {Effect} */ (reaction));
} }
@ -841,7 +841,7 @@ export function eager(fn) {
get(version); get(version);
inspect_effect(() => { eager_effect(() => {
if (initial) { if (initial) {
// the first time this runs, we create an inspect effect // the first time this runs, we create an inspect effect
// that will run eagerly whenever the expression changes // that will run eagerly whenever the expression changes
@ -918,14 +918,14 @@ export function fork(fn) {
// trigger any `$state.eager(...)` expressions with the new state // trigger any `$state.eager(...)` expressions with the new state
flushSync(() => { flushSync(() => {
/** @type {Set<Effect>} */ /** @type {Set<Effect>} */
const inspect_effects = new Set(); const eager_effects = new Set();
for (const source of batch.current.keys()) { for (const source of batch.current.keys()) {
mark_inspect_effects(source, inspect_effects); mark_eager_effects(source, eager_effects);
} }
set_inspect_effects(inspect_effects); set_eager_effects(eager_effects);
flush_inspect_effects(); flush_eager_effects();
}); });
batch.revive(); batch.revive();

@ -28,7 +28,7 @@ import { equals, safe_equals } from './equality.js';
import * as e from '../errors.js'; import * as e from '../errors.js';
import * as w from '../warnings.js'; import * as w from '../warnings.js';
import { async_effect, destroy_effect, teardown } from './effects.js'; import { async_effect, destroy_effect, teardown } from './effects.js';
import { inspect_effects, internal_set, set_inspect_effects, source } from './sources.js'; import { eager_effects, internal_set, set_eager_effects, source } from './sources.js';
import { get_stack } from '../dev/tracing.js'; import { get_stack } from '../dev/tracing.js';
import { async_mode_flag, tracing_mode_flag } from '../../flags/index.js'; import { async_mode_flag, tracing_mode_flag } from '../../flags/index.js';
import { Boundary } from '../dom/blocks/boundary.js'; import { Boundary } from '../dom/blocks/boundary.js';
@ -318,8 +318,8 @@ export function execute_derived(derived) {
set_active_effect(get_derived_parent_effect(derived)); set_active_effect(get_derived_parent_effect(derived));
if (DEV) { if (DEV) {
let prev_inspect_effects = inspect_effects; let prev_eager_effects = eager_effects;
set_inspect_effects(new Set()); set_eager_effects(new Set());
try { try {
if (stack.includes(derived)) { if (stack.includes(derived)) {
e.derived_references_self(); e.derived_references_self();
@ -332,7 +332,7 @@ export function execute_derived(derived) {
value = update_reaction(derived); value = update_reaction(derived);
} finally { } finally {
set_active_effect(prev_active_effect); set_active_effect(prev_active_effect);
set_inspect_effects(prev_inspect_effects); set_eager_effects(prev_eager_effects);
stack.pop(); stack.pop();
} }
} else { } else {

@ -27,7 +27,7 @@ import {
DERIVED, DERIVED,
UNOWNED, UNOWNED,
CLEAN, CLEAN,
INSPECT_EFFECT, EAGER_EFFECT,
HEAD_EFFECT, HEAD_EFFECT,
MAYBE_DIRTY, MAYBE_DIRTY,
EFFECT_PRESERVED, EFFECT_PRESERVED,
@ -88,7 +88,7 @@ function create_effect(type, fn, sync, push = true) {
if (DEV) { if (DEV) {
// Ensure the parent is never an inspect effect // Ensure the parent is never an inspect effect
while (parent !== null && (parent.f & INSPECT_EFFECT) !== 0) { while (parent !== null && (parent.f & EAGER_EFFECT) !== 0) {
parent = parent.parent; parent = parent.parent;
} }
} }
@ -242,8 +242,8 @@ export function user_pre_effect(fn) {
} }
/** @param {() => void | (() => void)} fn */ /** @param {() => void | (() => void)} fn */
export function inspect_effect(fn) { export function eager_effect(fn) {
return create_effect(INSPECT_EFFECT, fn, true); return create_effect(EAGER_EFFECT, fn, true);
} }
/** /**

@ -22,7 +22,7 @@ import {
DERIVED, DERIVED,
DIRTY, DIRTY,
BRANCH_EFFECT, BRANCH_EFFECT,
INSPECT_EFFECT, EAGER_EFFECT,
UNOWNED, UNOWNED,
MAYBE_DIRTY, MAYBE_DIRTY,
BLOCK_EFFECT, BLOCK_EFFECT,
@ -39,7 +39,7 @@ import { proxy } from '../proxy.js';
import { execute_derived } from './deriveds.js'; import { execute_derived } from './deriveds.js';
/** @type {Set<any>} */ /** @type {Set<any>} */
export let inspect_effects = new Set(); export let eager_effects = new Set();
/** @type {Map<Source, any>} */ /** @type {Map<Source, any>} */
export const old_values = new Map(); export const old_values = new Map();
@ -47,14 +47,14 @@ export const old_values = new Map();
/** /**
* @param {Set<any>} v * @param {Set<any>} v
*/ */
export function set_inspect_effects(v) { export function set_eager_effects(v) {
inspect_effects = v; eager_effects = v;
} }
let inspect_effects_deferred = false; let eager_effects_deferred = false;
export function set_inspect_effects_deferred() { export function set_eager_effects_deferred() {
inspect_effects_deferred = true; eager_effects_deferred = true;
} }
/** /**
@ -146,9 +146,9 @@ export function set(source, value, should_proxy = false) {
active_reaction !== null && active_reaction !== null &&
// since we are untracking the function inside `$inspect.with` we need to add this check // since we are untracking the function inside `$inspect.with` we need to add this check
// to ensure we error if state is set inside an inspect effect // to ensure we error if state is set inside an inspect effect
(!untracking || (active_reaction.f & INSPECT_EFFECT) !== 0) && (!untracking || (active_reaction.f & EAGER_EFFECT) !== 0) &&
is_runes() && is_runes() &&
(active_reaction.f & (DERIVED | BLOCK_EFFECT | ASYNC | INSPECT_EFFECT)) !== 0 && (active_reaction.f & (DERIVED | BLOCK_EFFECT | ASYNC | EAGER_EFFECT)) !== 0 &&
!current_sources?.includes(source) !current_sources?.includes(source)
) { ) {
e.state_unsafe_mutation(); e.state_unsafe_mutation();
@ -235,18 +235,18 @@ export function internal_set(source, value) {
} }
} }
if (DEV && !batch.is_fork && inspect_effects.size > 0 && !inspect_effects_deferred) { if (!batch.is_fork && eager_effects.size > 0 && !eager_effects_deferred) {
flush_inspect_effects(); flush_eager_effects();
} }
} }
return value; return value;
} }
export function flush_inspect_effects() { export function flush_eager_effects() {
inspect_effects_deferred = false; eager_effects_deferred = false;
const inspects = Array.from(inspect_effects); const inspects = Array.from(eager_effects);
for (const effect of inspects) { for (const effect of inspects) {
// Mark clean inspect-effects as maybe dirty and then check their dirtiness // Mark clean inspect-effects as maybe dirty and then check their dirtiness
@ -260,7 +260,7 @@ export function flush_inspect_effects() {
} }
} }
inspect_effects.clear(); eager_effects.clear();
} }
/** /**
@ -320,8 +320,8 @@ function mark_reactions(signal, status) {
if (!runes && reaction === active_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 & EAGER_EFFECT) !== 0) {
inspect_effects.add(reaction); eager_effects.add(reaction);
continue; continue;
} }

Loading…
Cancel
Save