Skip to content

chore: simplify signal capturing logic #14281

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
Nov 13, 2024
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
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/reactivity/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { derived, derived_safe_equal } from './deriveds.js';
import {
active_effect,
get,
is_signals_recorded,
captured_signals,
set_active_effect,
untrack,
update
Expand Down Expand Up @@ -390,7 +390,7 @@ export function prop(props, key, flags, fallback) {
return function (/** @type {any} */ value, /** @type {boolean} */ mutation) {
// legacy nonsense — need to ensure the source is invalidated when necessary
// also needed for when handling inspect logic so we can inspect the correct source signal
if (is_signals_recorded) {
if (captured_signals !== null) {
// set this so that we don't reset to the parent value if `d`
// is invalidated because of `invalidate_inner_signals` (rather
// than because the parent or child value changed)
Expand Down
13 changes: 5 additions & 8 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ let current_version = 0;
// 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
export let is_signals_recorded = false;
let captured_signals = new Set();
/** @type {Set<Value> | null} */
export let captured_signals = null;

// Handling runtime component context
/** @type {ComponentContext | null} */
Expand Down Expand Up @@ -732,7 +732,7 @@ export function get(signal) {
return value;
}

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

Expand Down Expand Up @@ -800,21 +800,18 @@ export function safe_get(signal) {
* @param {() => any} fn
*/
export function invalidate_inner_signals(fn) {
var previous_is_signals_recorded = is_signals_recorded;
var previous_captured_signals = captured_signals;
is_signals_recorded = true;
captured_signals = new Set();
var captured = captured_signals;
var signal;
try {
untrack(fn);
} finally {
is_signals_recorded = previous_is_signals_recorded;
if (is_signals_recorded) {
if (previous_captured_signals !== null) {
for (signal of captured_signals) {
previous_captured_signals.add(signal);
}
}
} finally {
captured_signals = previous_captured_signals;
}
for (signal of captured) {
Expand Down
Loading