Skip to content

Commit 38e6943

Browse files
committed
Merge branch 'main' into simplify-source-ownership
2 parents 802f90a + 8da222f commit 38e6943

File tree

13 files changed

+50
-104
lines changed

13 files changed

+50
-104
lines changed

.changeset/new-candles-marry.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 internal component `pop()`

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

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,14 @@ export function getAllContexts() {
139139
* @returns {void}
140140
*/
141141
export function push(props, runes = false, fn) {
142-
var ctx = (component_context = {
142+
component_context = {
143143
p: component_context,
144144
c: null,
145-
d: false,
146145
e: null,
147-
m: false,
148146
s: props,
149147
x: null,
150148
l: null
151-
});
149+
};
152150

153151
if (legacy_mode_flag && !runes) {
154152
component_context.l = {
@@ -159,10 +157,6 @@ export function push(props, runes = false, fn) {
159157
};
160158
}
161159

162-
teardown(() => {
163-
/** @type {ComponentContext} */ (ctx).d = true;
164-
});
165-
166160
if (DEV) {
167161
// component function
168162
component_context.function = fn;
@@ -176,37 +170,28 @@ export function push(props, runes = false, fn) {
176170
* @returns {T}
177171
*/
178172
export function pop(component) {
179-
const context_stack_item = component_context;
180-
if (context_stack_item !== null) {
181-
if (component !== undefined) {
182-
context_stack_item.x = component;
183-
}
184-
const component_effects = context_stack_item.e;
185-
if (component_effects !== null) {
186-
var previous_effect = active_effect;
187-
var previous_reaction = active_reaction;
188-
context_stack_item.e = null;
189-
try {
190-
for (var i = 0; i < component_effects.length; i++) {
191-
var component_effect = component_effects[i];
192-
set_active_effect(component_effect.effect);
193-
set_active_reaction(component_effect.reaction);
194-
create_user_effect(component_effect.fn);
195-
}
196-
} finally {
197-
set_active_effect(previous_effect);
198-
set_active_reaction(previous_reaction);
199-
}
200-
}
201-
component_context = context_stack_item.p;
202-
if (DEV) {
203-
dev_current_component_function = context_stack_item.p?.function ?? null;
173+
var context = /** @type {ComponentContext} */ (component_context);
174+
var effects = context.e;
175+
176+
if (effects !== null) {
177+
context.e = null;
178+
179+
for (var fn of effects) {
180+
create_user_effect(fn);
204181
}
205-
context_stack_item.m = true;
206182
}
207-
// Micro-optimization: Don't set .a above to the empty object
208-
// so it can be garbage-collected when the return here is unused
209-
return component || /** @type {T} */ ({});
183+
184+
if (component !== undefined) {
185+
context.x = component;
186+
}
187+
188+
component_context = context.p;
189+
190+
if (DEV) {
191+
dev_current_component_function = component_context?.function ?? null;
192+
}
193+
194+
return component ?? /** @type {T} */ ({});
210195
}
211196

212197
/** @returns {boolean} */

packages/svelte/src/internal/client/dom/elements/transitions.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,14 @@ export function transition(flags, element, get_fn, get_params) {
209209
var outro;
210210

211211
function get_options() {
212-
var previous_reaction = active_reaction;
213-
var previous_effect = active_effect;
214-
set_active_reaction(null);
215-
set_active_effect(null);
216-
try {
212+
return without_reactive_context(() => {
217213
// If a transition is still ongoing, we use the existing options rather than generating
218214
// new ones. This ensures that reversible transitions reverse smoothly, rather than
219215
// jumping to a new spot because (for example) a different `duration` was used
220216
return (current_options ??= get_fn()(element, get_params?.() ?? /** @type {P} */ ({}), {
221217
direction
222218
}));
223-
} finally {
224-
set_active_reaction(previous_reaction);
225-
set_active_effect(previous_effect);
226-
}
219+
});
227220
}
228221

229222
/** @type {TransitionManager} */

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
is_array,
99
object_prototype
1010
} from '../shared/utils.js';
11-
import { state as source, set } from './reactivity/sources.js';
11+
import { state as source, set, increment } from './reactivity/sources.js';
1212
import { PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
1313
import { UNINITIALIZED } from '../../constants.js';
1414
import * as e from './errors.js';
@@ -118,7 +118,7 @@ export function proxy(value) {
118118
if (prop in target) {
119119
const s = with_parent(() => source(UNINITIALIZED, stack));
120120
sources.set(prop, s);
121-
update_version(version);
121+
increment(version);
122122

123123
if (DEV) {
124124
tag(s, get_label(path, prop));
@@ -136,7 +136,7 @@ export function proxy(value) {
136136
}
137137
}
138138
set(s, UNINITIALIZED);
139-
update_version(version);
139+
increment(version);
140140
}
141141

142142
return true;
@@ -304,7 +304,7 @@ export function proxy(value) {
304304
}
305305
}
306306

307-
update_version(version);
307+
increment(version);
308308
}
309309

310310
return true;
@@ -343,14 +343,6 @@ function get_label(path, prop) {
343343
return /^\d+$/.test(prop) ? `${path}[${prop}]` : `${path}['${prop}']`;
344344
}
345345

346-
/**
347-
* @param {Source<number>} signal
348-
* @param {1 | -1} [d]
349-
*/
350-
function update_version(signal, d = 1) {
351-
set(signal, signal.v + d);
352-
}
353-
354346
/**
355347
* @param {any} value
356348
*/

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,28 +179,18 @@ export function teardown(fn) {
179179
export function user_effect(fn) {
180180
validate_effect('$effect');
181181

182-
// Non-nested `$effect(...)` in a component should be deferred
183-
// until the component is mounted
184-
var defer =
185-
active_effect !== null &&
186-
(active_effect.f & BRANCH_EFFECT) !== 0 &&
187-
component_context !== null &&
188-
!component_context.m;
189-
190182
if (DEV) {
191183
define_property(fn, 'name', {
192184
value: '$effect'
193185
});
194186
}
195187

196-
if (defer) {
188+
if (!active_reaction && active_effect && (active_effect.f & BRANCH_EFFECT) !== 0) {
189+
// Top-level `$effect(...)` in a component — defer until mount
197190
var context = /** @type {ComponentContext} */ (component_context);
198-
(context.e ??= []).push({
199-
fn,
200-
effect: active_effect,
201-
reaction: active_reaction
202-
});
191+
(context.e ??= []).push(fn);
203192
} else {
193+
// Everything else — create immediately
204194
return create_user_effect(fn);
205195
}
206196
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,6 @@ export function spread_props(...props) {
268268
return new Proxy({ props }, spread_props_handler);
269269
}
270270

271-
/**
272-
* @param {Derived} current_value
273-
* @returns {boolean}
274-
*/
275-
function has_destroyed_component_ctx(current_value) {
276-
return current_value.ctx?.d ?? false;
277-
}
278-
279271
/**
280272
* This function is responsible for synchronizing a possibly bound prop with the inner component state.
281273
* It is used whenever the compiler sees that the component writes to the prop, or when it has a default prop_value.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ export function update_pre(source, d = 1) {
259259
return set(source, d === 1 ? ++value : --value);
260260
}
261261

262+
/**
263+
* Silently (without using `get`) increment a source
264+
* @param {Source<number>} source
265+
*/
266+
export function increment(source) {
267+
set(source, source.v + 1);
268+
}
269+
262270
/**
263271
* @param {Value} signal
264272
* @param {number} status should be DIRTY or MAYBE_DIRTY

packages/svelte/src/internal/client/types.d.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,8 @@ export type ComponentContext = {
1414
p: null | ComponentContext;
1515
/** context */
1616
c: null | Map<unknown, unknown>;
17-
/** destroyed */
18-
d: boolean;
1917
/** deferred effects */
20-
e: null | Array<{
21-
fn: () => void | (() => void);
22-
effect: null | Effect;
23-
reaction: null | Reaction;
24-
}>;
25-
/** mounted */
26-
m: boolean;
18+
e: null | Array<() => void | (() => void)>;
2719
/**
2820
* props — needed for legacy mode lifecycle functions, and for `createEventDispatcher`
2921
* @deprecated remove in 6.0

packages/svelte/src/reactivity/create-subscriber.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { get, tick, untrack } from '../internal/client/runtime.js';
22
import { effect_tracking, render_effect } from '../internal/client/reactivity/effects.js';
3-
import { source } from '../internal/client/reactivity/sources.js';
3+
import { source, increment } from '../internal/client/reactivity/sources.js';
44
import { tag } from '../internal/client/dev/tracing.js';
5-
import { increment } from './utils.js';
65
import { DEV } from 'esm-env';
76

87
/**

packages/svelte/src/reactivity/map.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/** @import { Source } from '#client' */
22
import { DEV } from 'esm-env';
3-
import { set, source, state } from '../internal/client/reactivity/sources.js';
3+
import { set, source, state, increment } from '../internal/client/reactivity/sources.js';
44
import { label, tag } from '../internal/client/dev/tracing.js';
55
import { get, update_version } from '../internal/client/runtime.js';
6-
import { increment } from './utils.js';
76

87
/**
98
* A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.

0 commit comments

Comments
 (0)