Skip to content

Commit 6f0aec5

Browse files
authored
chore: simplify source ownership (#16333)
* simplify source ownership * rename * changeset * make it unnecessary to hand onto `current_sources` past the initial update
1 parent 443e76e commit 6f0aec5

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

.changeset/eight-walls-mate.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: simplify reaction/source ownership tracking

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
/** @import { Source } from '#client' */
22
import { DEV } from 'esm-env';
3-
import { get, active_effect, active_reaction, set_active_reaction } from './runtime.js';
3+
import {
4+
get,
5+
active_effect,
6+
update_version,
7+
active_reaction,
8+
set_update_version,
9+
set_active_reaction
10+
} from './runtime.js';
411
import {
512
array_prototype,
613
get_descriptor,
@@ -41,21 +48,31 @@ export function proxy(value) {
4148
var version = source(0);
4249

4350
var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
44-
var reaction = active_reaction;
51+
var parent_version = update_version;
4552

4653
/**
4754
* Executes the proxy in the context of the reaction it was originally created in, if any
4855
* @template T
4956
* @param {() => T} fn
5057
*/
5158
var with_parent = (fn) => {
52-
var previous_reaction = active_reaction;
53-
set_active_reaction(reaction);
59+
if (update_version === parent_version) {
60+
return fn();
61+
}
62+
63+
// child source is being created after the initial proxy —
64+
// prevent it from being associated with the current reaction
65+
var reaction = active_reaction;
66+
var version = update_version;
67+
68+
set_active_reaction(null);
69+
set_update_version(parent_version);
5470

55-
/** @type {T} */
5671
var result = fn();
5772

58-
set_active_reaction(previous_reaction);
73+
set_active_reaction(reaction);
74+
set_update_version(version);
75+
5976
return result;
6077
};
6178

packages/svelte/src/internal/client/reactivity/sources.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
untrack,
1212
increment_write_version,
1313
update_effect,
14-
source_ownership,
14+
current_sources,
1515
check_dirtiness,
1616
untracking,
1717
is_destroying_effect,
@@ -140,7 +140,7 @@ export function set(source, value, should_proxy = false) {
140140
(!untracking || (active_reaction.f & INSPECT_EFFECT) !== 0) &&
141141
is_runes() &&
142142
(active_reaction.f & (DERIVED | BLOCK_EFFECT | INSPECT_EFFECT)) !== 0 &&
143-
!(source_ownership?.reaction === active_reaction && source_ownership.sources.includes(source))
143+
!current_sources?.includes(source)
144144
) {
145145
e.state_unsafe_mutation();
146146
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ export function set_active_effect(effect) {
8888
/**
8989
* When sources are created within a reaction, reading and writing
9090
* them within that reaction should not cause a re-run
91-
* @type {null | { reaction: Reaction, sources: Source[] }}
91+
* @type {null | Source[]}
9292
*/
93-
export let source_ownership = null;
93+
export let current_sources = null;
9494

9595
/** @param {Value} value */
9696
export function push_reaction_value(value) {
9797
if (active_reaction !== null && active_reaction.f & EFFECT_IS_UPDATING) {
98-
if (source_ownership === null) {
99-
source_ownership = { reaction: active_reaction, sources: [value] };
98+
if (current_sources === null) {
99+
current_sources = [value];
100100
} else {
101-
source_ownership.sources.push(value);
101+
current_sources.push(value);
102102
}
103103
}
104104
}
@@ -136,6 +136,11 @@ let read_version = 0;
136136

137137
export let update_version = read_version;
138138

139+
/** @param {number} value */
140+
export function set_update_version(value) {
141+
update_version = value;
142+
}
143+
139144
// If we are working with a get() chain that has no active container,
140145
// to prevent memory leaks, we skip adding the reaction.
141146
export let skip_reaction = false;
@@ -236,7 +241,7 @@ function schedule_possible_effect_self_invalidation(signal, effect, root = true)
236241
var reactions = signal.reactions;
237242
if (reactions === null) return;
238243

239-
if (source_ownership?.reaction === active_reaction && source_ownership.sources.includes(signal)) {
244+
if (current_sources?.includes(signal)) {
240245
return;
241246
}
242247

@@ -263,7 +268,7 @@ export function update_reaction(reaction) {
263268
var previous_untracked_writes = untracked_writes;
264269
var previous_reaction = active_reaction;
265270
var previous_skip_reaction = skip_reaction;
266-
var previous_reaction_sources = source_ownership;
271+
var previous_sources = current_sources;
267272
var previous_component_context = component_context;
268273
var previous_untracking = untracking;
269274
var previous_update_version = update_version;
@@ -277,7 +282,7 @@ export function update_reaction(reaction) {
277282
(flags & UNOWNED) !== 0 && (untracking || !is_updating_effect || active_reaction === null);
278283
active_reaction = (flags & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null;
279284

280-
source_ownership = null;
285+
current_sources = null;
281286
set_component_context(reaction.ctx);
282287
untracking = false;
283288
update_version = ++read_version;
@@ -365,7 +370,7 @@ export function update_reaction(reaction) {
365370
untracked_writes = previous_untracked_writes;
366371
active_reaction = previous_reaction;
367372
skip_reaction = previous_skip_reaction;
368-
source_ownership = previous_reaction_sources;
373+
current_sources = previous_sources;
369374
set_component_context(previous_component_context);
370375
untracking = previous_untracking;
371376
update_version = previous_update_version;
@@ -759,10 +764,7 @@ export function get(signal) {
759764

760765
// Register the dependency on the current reaction signal.
761766
if (active_reaction !== null && !untracking) {
762-
if (
763-
source_ownership?.reaction !== active_reaction ||
764-
!source_ownership?.sources.includes(signal)
765-
) {
767+
if (!current_sources?.includes(signal)) {
766768
var deps = active_reaction.deps;
767769
if (signal.rv < read_version) {
768770
signal.rv = read_version;

0 commit comments

Comments
 (0)