Skip to content

chore: move capture_signals to legacy module #16456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
chore: move capture_signals to legacy module
  • Loading branch information
Rich-Harris committed Jul 20, 2025
commit ae148be0d15e856bf34a66fc63d1e37d478762aa
5 changes: 5 additions & 0 deletions .changeset/swift-cherries-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: move `capture_signals` to legacy module
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dev/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { snapshot } from '../../shared/clone.js';
import { define_property } from '../../shared/utils.js';
import { DERIVED, ASYNC, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
import { effect_tracking } from '../reactivity/effects.js';
import { active_reaction, captured_signals, set_captured_signals, untrack } from '../runtime.js';
import { active_reaction, untrack } from '../runtime.js';

/**
* @typedef {{
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ export {
mark_store_binding
} from './reactivity/store.js';
export { boundary, pending } from './dom/blocks/boundary.js';
export { invalidate_inner_signals } from './legacy.js';
export { set_text } from './render.js';
export {
get,
safe_get,
invalidate_inner_signals,
tick,
untrack,
exclude_from_object,
Expand Down
46 changes: 46 additions & 0 deletions packages/svelte/src/internal/client/legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** @import { Value } from '#client' */
import { internal_set } from './reactivity/sources.js';
import { untrack } from './runtime.js';

/**
* @type {Set<Value> | null}
* @deprecated
*/
export let captured_signals = null;

/**
* Capture an array of all the signals that are read when `fn` is called
* @template T
* @param {() => T} fn
*/
function capture_signals(fn) {
var previous_captured_signals = captured_signals;

try {
captured_signals = new Set();

untrack(fn);

if (previous_captured_signals !== null) {
for (var signal of captured_signals) {
previous_captured_signals.add(signal);
}
}

return captured_signals;
} finally {
captured_signals = previous_captured_signals;
}
}

/**
* Invokes a function and captures all signals that are read during the invocation,
* then invalidates them.
* @param {() => any} fn
* @deprecated
*/
export function invalidate_inner_signals(fn) {
for (var signal of capture_signals(fn)) {
internal_set(signal, signal.v);
}
}
54 changes: 3 additions & 51 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
STALE_REACTION,
ERROR_VALUE
} from './constants.js';
import { internal_set, old_values } from './reactivity/sources.js';
import { old_values } from './reactivity/sources.js';
import {
destroy_derived_effects,
execute_derived,
Expand All @@ -45,6 +45,7 @@ import * as w from './warnings.js';
import { Batch, batch_deriveds, flushSync, schedule_effect } from './reactivity/batch.js';
import { handle_error } from './error-handling.js';
import { UNINITIALIZED } from '../../constants.js';
import { captured_signals } from './legacy.js';

export let is_updating_effect = false;

Expand Down Expand Up @@ -137,14 +138,6 @@ export function set_update_version(value) {
// If we are working with a get() chain that has no active container,
// to prevent memory leaks, we skip adding the reaction.
export let skip_reaction = false;
// Handle collecting all signals which are read during a specific time frame
/** @type {Set<Value> | null} */
export let captured_signals = null;

/** @param {Set<Value> | null} value */
export function set_captured_signals(value) {
captured_signals = value;
}

export function increment_write_version() {
return ++write_version;
Expand Down Expand Up @@ -531,9 +524,7 @@ export function get(signal) {
var flags = signal.f;
var is_derived = (flags & DERIVED) !== 0;

if (captured_signals !== null) {
captured_signals.add(signal);
}
captured_signals?.add(signal);

// Register the dependency on the current reaction signal.
if (active_reaction !== null && !untracking) {
Expand Down Expand Up @@ -713,45 +704,6 @@ export function safe_get(signal) {
return signal && get(signal);
}

/**
* Capture an array of all the signals that are read when `fn` is called
* @template T
* @param {() => T} fn
*/
function capture_signals(fn) {
var previous_captured_signals = captured_signals;
captured_signals = new Set();

var captured = captured_signals;
var signal;

try {
untrack(fn);
if (previous_captured_signals !== null) {
for (signal of captured_signals) {
previous_captured_signals.add(signal);
}
}
} finally {
captured_signals = previous_captured_signals;
}

return captured;
}

/**
* Invokes a function and captures all signals that are read during the invocation,
* then invalidates them.
* @param {() => any} fn
*/
export function invalidate_inner_signals(fn) {
var captured = capture_signals(() => untrack(fn));

for (var signal of captured) {
internal_set(signal, signal.v);
}
}

/**
* When used inside a [`$derived`](https://svelte.dev/docs/svelte/$derived) or [`$effect`](https://svelte.dev/docs/svelte/$effect),
* any state read inside `fn` will not be treated as a dependency.
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy