Skip to content
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/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,
reaction_sources,
source_ownership,
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 &&
!(reaction_sources?.[1].includes(source) && reaction_sources[0] === active_reaction)
!(source_ownership?.reaction === active_reaction && source_ownership.sources.includes(source))
) {
e.state_unsafe_mutation();
}
Expand Down
28 changes: 18 additions & 10 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ 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 | [active_reaction: Reaction, sources: Source[]]}
* @type {null | { reaction: Reaction, sources: Source[] }}
*/
export let reaction_sources = null;
export let source_ownership = null;

/** @param {Value} value */
export function push_reaction_value(value) {
if (active_reaction !== null && active_reaction.f & EFFECT_IS_UPDATING) {
if (reaction_sources === null) {
reaction_sources = [active_reaction, [value]];
if (source_ownership === null) {
source_ownership = { reaction: active_reaction, sources: [value] };
} else {
reaction_sources[1].push(value);
source_ownership.sources.push(value);
}
}
}
Expand Down Expand Up @@ -235,7 +235,12 @@ function schedule_possible_effect_self_invalidation(signal, effect, root = true)
for (var i = 0; i < reactions.length; i++) {
var reaction = reactions[i];

if (reaction_sources?.[1].includes(signal) && reaction_sources[0] === active_reaction) continue;
if (
source_ownership?.reaction === active_reaction &&
source_ownership.sources.includes(signal)
) {
continue;
}

if ((reaction.f & DERIVED) !== 0) {
schedule_possible_effect_self_invalidation(/** @type {Derived} */ (reaction), effect, false);
Expand All @@ -257,7 +262,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 = reaction_sources;
var previous_reaction_sources = source_ownership;
var previous_component_context = component_context;
var previous_untracking = untracking;

Expand All @@ -270,7 +275,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;

reaction_sources = null;
source_ownership = null;
set_component_context(reaction.ctx);
untracking = false;
read_version++;
Expand Down Expand Up @@ -358,7 +363,7 @@ export function update_reaction(reaction) {
untracked_writes = previous_untracked_writes;
active_reaction = previous_reaction;
skip_reaction = previous_skip_reaction;
reaction_sources = previous_reaction_sources;
source_ownership = previous_reaction_sources;
set_component_context(previous_component_context);
untracking = previous_untracking;

Expand Down Expand Up @@ -739,7 +744,10 @@ export function get(signal) {

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