Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/eight-walls-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: simplify reaction/source ownership tracking
10 changes: 5 additions & 5 deletions packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @import { Source } from '#client' */
import { DEV } from 'esm-env';
import { get, active_effect, active_reaction, set_active_reaction } from './runtime.js';
import { get, active_effect, current_sources, set_current_sources } from './runtime.js';
import {
array_prototype,
get_descriptor,
Expand Down Expand Up @@ -41,21 +41,21 @@ export function proxy(value) {
var version = source(0);

var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
var reaction = active_reaction;
var parent_sources = current_sources;

/**
* Executes the proxy in the context of the reaction it was originally created in, if any
* @template T
* @param {() => T} fn
*/
var with_parent = (fn) => {
var previous_reaction = active_reaction;
set_active_reaction(reaction);
var previous_sources = current_sources;
set_current_sources(parent_sources);

/** @type {T} */
var result = fn();

set_active_reaction(previous_reaction);
set_current_sources(previous_sources);
return result;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
untrack,
increment_write_version,
update_effect,
source_ownership,
current_sources,
check_dirtiness,
untracking,
is_destroying_effect,
Expand Down Expand Up @@ -140,7 +140,7 @@ export function set(source, value, should_proxy = false) {
(!untracking || (active_reaction.f & INSPECT_EFFECT) !== 0) &&
is_runes() &&
(active_reaction.f & (DERIVED | BLOCK_EFFECT | INSPECT_EFFECT)) !== 0 &&
!(source_ownership?.reaction === active_reaction && source_ownership.sources.includes(source))
!current_sources?.includes(source)
) {
e.state_unsafe_mutation();
}
Expand Down
30 changes: 17 additions & 13 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,24 @@ export function set_active_effect(effect) {
/**
* When sources are created within a reaction, reading and writing
* them within that reaction should not cause a re-run
* @type {null | { reaction: Reaction, sources: Source[] }}
* @type {null | Source[]}
*/
export let source_ownership = null;
export let current_sources = null;

/**
* @param {null | Source[]} value
*/
export function set_current_sources(value) {
current_sources = value;
}

/** @param {Value} value */
export function push_reaction_value(value) {
if (active_reaction !== null && active_reaction.f & EFFECT_IS_UPDATING) {
if (source_ownership === null) {
source_ownership = { reaction: active_reaction, sources: [value] };
if (current_sources === null) {
current_sources = [value];
} else {
source_ownership.sources.push(value);
current_sources.push(value);
}
}
}
Expand Down Expand Up @@ -236,7 +243,7 @@ function schedule_possible_effect_self_invalidation(signal, effect, root = true)
var reactions = signal.reactions;
if (reactions === null) return;

if (source_ownership?.reaction === active_reaction && source_ownership.sources.includes(signal)) {
if (current_sources?.includes(signal)) {
return;
}

Expand All @@ -263,7 +270,7 @@ export function update_reaction(reaction) {
var previous_untracked_writes = untracked_writes;
var previous_reaction = active_reaction;
var previous_skip_reaction = skip_reaction;
var previous_reaction_sources = source_ownership;
var previous_sources = current_sources;
var previous_component_context = component_context;
var previous_untracking = untracking;
var previous_update_version = update_version;
Expand All @@ -277,7 +284,7 @@ export function update_reaction(reaction) {
(flags & UNOWNED) !== 0 && (untracking || !is_updating_effect || active_reaction === null);
active_reaction = (flags & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null;

source_ownership = null;
current_sources = null;
set_component_context(reaction.ctx);
untracking = false;
update_version = ++read_version;
Expand Down Expand Up @@ -365,7 +372,7 @@ export function update_reaction(reaction) {
untracked_writes = previous_untracked_writes;
active_reaction = previous_reaction;
skip_reaction = previous_skip_reaction;
source_ownership = previous_reaction_sources;
current_sources = previous_sources;
set_component_context(previous_component_context);
untracking = previous_untracking;
update_version = previous_update_version;
Expand Down Expand Up @@ -759,10 +766,7 @@ export function get(signal) {

// Register the dependency on the current reaction signal.
if (active_reaction !== null && !untracking) {
if (
source_ownership?.reaction !== active_reaction ||
!source_ownership?.sources.includes(signal)
) {
if (!current_sources?.includes(signal)) {
var deps = active_reaction.deps;
if (signal.rv < read_version) {
signal.rv = read_version;
Expand Down