Skip to content

Commit 1f99914

Browse files
authored
chore: move capture_signals to legacy module (#16456)
1 parent d3a01bd commit 1f99914

File tree

5 files changed

+56
-53
lines changed

5 files changed

+56
-53
lines changed

.changeset/swift-cherries-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: move `capture_signals` to legacy module

packages/svelte/src/internal/client/dev/tracing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { snapshot } from '../../shared/clone.js';
44
import { define_property } from '../../shared/utils.js';
55
import { DERIVED, ASYNC, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
66
import { effect_tracking } from '../reactivity/effects.js';
7-
import { active_reaction, captured_signals, set_captured_signals, untrack } from '../runtime.js';
7+
import { active_reaction, untrack } from '../runtime.js';
88

99
/**
1010
* @typedef {{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ export {
138138
mark_store_binding
139139
} from './reactivity/store.js';
140140
export { boundary, pending } from './dom/blocks/boundary.js';
141+
export { invalidate_inner_signals } from './legacy.js';
141142
export { set_text } from './render.js';
142143
export {
143144
get,
144145
safe_get,
145-
invalidate_inner_signals,
146146
tick,
147147
untrack,
148148
exclude_from_object,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/** @import { Value } from '#client' */
2+
import { internal_set } from './reactivity/sources.js';
3+
import { untrack } from './runtime.js';
4+
5+
/**
6+
* @type {Set<Value> | null}
7+
* @deprecated
8+
*/
9+
export let captured_signals = null;
10+
11+
/**
12+
* Capture an array of all the signals that are read when `fn` is called
13+
* @template T
14+
* @param {() => T} fn
15+
*/
16+
function capture_signals(fn) {
17+
var previous_captured_signals = captured_signals;
18+
19+
try {
20+
captured_signals = new Set();
21+
22+
untrack(fn);
23+
24+
if (previous_captured_signals !== null) {
25+
for (var signal of captured_signals) {
26+
previous_captured_signals.add(signal);
27+
}
28+
}
29+
30+
return captured_signals;
31+
} finally {
32+
captured_signals = previous_captured_signals;
33+
}
34+
}
35+
36+
/**
37+
* Invokes a function and captures all signals that are read during the invocation,
38+
* then invalidates them.
39+
* @param {() => any} fn
40+
* @deprecated
41+
*/
42+
export function invalidate_inner_signals(fn) {
43+
for (var signal of capture_signals(fn)) {
44+
internal_set(signal, signal.v);
45+
}
46+
}

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

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
STALE_REACTION,
2323
ERROR_VALUE
2424
} from './constants.js';
25-
import { internal_set, old_values } from './reactivity/sources.js';
25+
import { old_values } from './reactivity/sources.js';
2626
import {
2727
destroy_derived_effects,
2828
execute_derived,
@@ -45,6 +45,7 @@ import * as w from './warnings.js';
4545
import { Batch, batch_deriveds, flushSync, schedule_effect } from './reactivity/batch.js';
4646
import { handle_error } from './error-handling.js';
4747
import { UNINITIALIZED } from '../../constants.js';
48+
import { captured_signals } from './legacy.js';
4849

4950
export let is_updating_effect = false;
5051

@@ -137,14 +138,6 @@ export function set_update_version(value) {
137138
// If we are working with a get() chain that has no active container,
138139
// to prevent memory leaks, we skip adding the reaction.
139140
export let skip_reaction = false;
140-
// Handle collecting all signals which are read during a specific time frame
141-
/** @type {Set<Value> | null} */
142-
export let captured_signals = null;
143-
144-
/** @param {Set<Value> | null} value */
145-
export function set_captured_signals(value) {
146-
captured_signals = value;
147-
}
148141

149142
export function increment_write_version() {
150143
return ++write_version;
@@ -531,9 +524,7 @@ export function get(signal) {
531524
var flags = signal.f;
532525
var is_derived = (flags & DERIVED) !== 0;
533526

534-
if (captured_signals !== null) {
535-
captured_signals.add(signal);
536-
}
527+
captured_signals?.add(signal);
537528

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

716-
/**
717-
* Capture an array of all the signals that are read when `fn` is called
718-
* @template T
719-
* @param {() => T} fn
720-
*/
721-
function capture_signals(fn) {
722-
var previous_captured_signals = captured_signals;
723-
captured_signals = new Set();
724-
725-
var captured = captured_signals;
726-
var signal;
727-
728-
try {
729-
untrack(fn);
730-
if (previous_captured_signals !== null) {
731-
for (signal of captured_signals) {
732-
previous_captured_signals.add(signal);
733-
}
734-
}
735-
} finally {
736-
captured_signals = previous_captured_signals;
737-
}
738-
739-
return captured;
740-
}
741-
742-
/**
743-
* Invokes a function and captures all signals that are read during the invocation,
744-
* then invalidates them.
745-
* @param {() => any} fn
746-
*/
747-
export function invalidate_inner_signals(fn) {
748-
var captured = capture_signals(() => untrack(fn));
749-
750-
for (var signal of captured) {
751-
internal_set(signal, signal.v);
752-
}
753-
}
754-
755707
/**
756708
* When used inside a [`$derived`](https://svelte.dev/docs/svelte/$derived) or [`$effect`](https://svelte.dev/docs/svelte/$effect),
757709
* any state read inside `fn` will not be treated as a dependency.

0 commit comments

Comments
 (0)