Skip to content

Commit 5a8ec2e

Browse files
committed
simplify source ownership
1 parent 71ed9e4 commit 5a8ec2e

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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 { get, active_effect, current_sources, set_current_sources } from './runtime.js';
44
import {
55
array_prototype,
66
get_descriptor,
@@ -41,21 +41,21 @@ export function proxy(value) {
4141
var version = source(0);
4242

4343
var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
44-
var reaction = active_reaction;
44+
var parent_sources = current_sources;
4545

4646
/**
4747
* Executes the proxy in the context of the reaction it was originally created in, if any
4848
* @template T
4949
* @param {() => T} fn
5050
*/
5151
var with_parent = (fn) => {
52-
var previous_reaction = active_reaction;
53-
set_active_reaction(reaction);
52+
var previous_source_ownership = current_sources;
53+
set_current_sources(parent_sources);
5454

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

58-
set_active_reaction(previous_reaction);
58+
set_current_sources(previous_source_ownership);
5959
return result;
6060
};
6161

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: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,24 @@ 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;
94+
95+
/**
96+
* @param {null | Source[]} value
97+
*/
98+
export function set_current_sources(value) {
99+
current_sources = value;
100+
}
94101

95102
/** @param {Value} value */
96103
export function push_reaction_value(value) {
97104
if (active_reaction !== null && active_reaction.f & EFFECT_IS_UPDATING) {
98-
if (source_ownership === null) {
99-
source_ownership = { reaction: active_reaction, sources: [value] };
105+
if (current_sources === null) {
106+
current_sources = [value];
100107
} else {
101-
source_ownership.sources.push(value);
108+
current_sources.push(value);
102109
}
103110
}
104111
}
@@ -236,7 +243,7 @@ function schedule_possible_effect_self_invalidation(signal, effect, root = true)
236243
var reactions = signal.reactions;
237244
if (reactions === null) return;
238245

239-
if (source_ownership?.reaction === active_reaction && source_ownership.sources.includes(signal)) {
246+
if (current_sources?.includes(signal)) {
240247
return;
241248
}
242249

@@ -263,7 +270,7 @@ export function update_reaction(reaction) {
263270
var previous_untracked_writes = untracked_writes;
264271
var previous_reaction = active_reaction;
265272
var previous_skip_reaction = skip_reaction;
266-
var previous_reaction_sources = source_ownership;
273+
var previous_sources = current_sources;
267274
var previous_component_context = component_context;
268275
var previous_untracking = untracking;
269276
var previous_update_version = update_version;
@@ -277,7 +284,7 @@ export function update_reaction(reaction) {
277284
(flags & UNOWNED) !== 0 && (untracking || !is_updating_effect || active_reaction === null);
278285
active_reaction = (flags & (BRANCH_EFFECT | ROOT_EFFECT)) === 0 ? reaction : null;
279286

280-
source_ownership = null;
287+
current_sources = null;
281288
set_component_context(reaction.ctx);
282289
untracking = false;
283290
update_version = ++read_version;
@@ -365,7 +372,7 @@ export function update_reaction(reaction) {
365372
untracked_writes = previous_untracked_writes;
366373
active_reaction = previous_reaction;
367374
skip_reaction = previous_skip_reaction;
368-
source_ownership = previous_reaction_sources;
375+
current_sources = previous_sources;
369376
set_component_context(previous_component_context);
370377
untracking = previous_untracking;
371378
update_version = previous_update_version;
@@ -759,10 +766,7 @@ export function get(signal) {
759766

760767
// Register the dependency on the current reaction signal.
761768
if (active_reaction !== null && !untracking) {
762-
if (
763-
source_ownership?.reaction !== active_reaction ||
764-
!source_ownership?.sources.includes(signal)
765-
) {
769+
if (!current_sources?.includes(signal)) {
766770
var deps = active_reaction.deps;
767771
if (signal.rv < read_version) {
768772
signal.rv = read_version;

0 commit comments

Comments
 (0)