Skip to content

Commit 5cc1d14

Browse files
committed
simplify
1 parent a339890 commit 5cc1d14

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ import {
1010
import { get_descriptor, is_function } from '../../shared/utils.js';
1111
import { mutable_source, set, source, update } from './sources.js';
1212
import { derived, derived_safe_equal } from './deriveds.js';
13-
import { active_effect, get, captured_signals, set_active_effect, untrack } from '../runtime.js';
13+
import {
14+
active_effect,
15+
get,
16+
captured_signals,
17+
set_active_effect,
18+
untrack,
19+
active_reaction,
20+
set_active_reaction
21+
} from '../runtime.js';
1422
import { safe_equals } from './equality.js';
1523
import * as e from '../errors.js';
1624
import {
@@ -241,6 +249,29 @@ export function spread_props(...props) {
241249
return new Proxy({ props }, spread_props_handler);
242250
}
243251

252+
/**
253+
* @template T
254+
* @param {() => T} fn
255+
* @returns {T}
256+
*/
257+
function with_parent_branch(fn) {
258+
var effect = active_effect;
259+
var previous_effect = active_effect;
260+
var previous_reaction = active_reaction;
261+
262+
while (effect !== null && (effect.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0) {
263+
effect = effect.parent;
264+
}
265+
try {
266+
set_active_effect(effect);
267+
set_active_reaction(effect);
268+
return fn();
269+
} finally {
270+
set_active_effect(previous_effect);
271+
set_active_reaction(previous_reaction);
272+
}
273+
}
274+
244275
/**
245276
* This function is responsible for synchronizing a possibly bound prop with the inner component state.
246277
* It is used whenever the compiler sees that the component writes to the prop, or when it has a default prop_value.
@@ -259,11 +290,13 @@ export function prop(props, key, flags, fallback) {
259290
var is_store_sub = false;
260291
var prop_value;
261292

262-
if (bindable) {
263-
[prop_value, is_store_sub] = capture_store_binding(() => /** @type {V} */ (props[key]));
264-
} else {
265-
prop_value = /** @type {V} */ (props[key]);
266-
}
293+
with_parent_branch(() => {
294+
if (bindable) {
295+
[prop_value, is_store_sub] = capture_store_binding(() => /** @type {V} */ (props[key]));
296+
} else {
297+
prop_value = /** @type {V} */ (props[key]);
298+
}
299+
});
267300

268301
// Can be the case when someone does `mount(Component, props)` with `let props = $state({...})`
269302
// or `createClassComponent(Component, props)`

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,29 +184,19 @@ export function check_dirtiness(reaction) {
184184
// If we are working with a disconnected or an unowned signal that is now connected (due to an active effect)
185185
// then we need to re-connect the reaction to the dependency
186186
if (is_disconnected || is_unowned_connected) {
187-
var derived = /** @type {Derived} */ (reaction);
188-
189187
for (i = 0; i < length; i++) {
190188
dependency = dependencies[i];
191189

192190
// We always re-add all reactions (even duplicates) if the derived was
193191
// previously disconnected, however we don't if it was unowned as we
194192
// de-duplicate dependencies in that case
195-
if (is_disconnected || !dependency?.reactions?.includes(derived)) {
196-
(dependency.reactions ??= []).push(derived);
193+
if (is_disconnected || !dependency?.reactions?.includes(reaction)) {
194+
(dependency.reactions ??= []).push(reaction);
197195
}
198196
}
199197

200198
if (is_disconnected) {
201-
derived.f ^= DISCONNECTED;
202-
}
203-
var parent = derived.parent;
204-
205-
if (is_unowned_connected && parent !== null && (parent.f & UNOWNED) === 0) {
206-
// If the derived is owned by another derived then mark it as owned agaub
207-
// as the derived value might have been referenced in a different context
208-
// and now has a reacive context managing it
209-
// derived.f ^= UNOWNED;
199+
reaction.f ^= DISCONNECTED;
210200
}
211201
}
212202

0 commit comments

Comments
 (0)