Skip to content

Commit 8da222f

Browse files
authored
chore: simplify component pop (#16331)
* chore: simplify pop * shuffle * remove .m flag on component context * unused * shuffle * simplify * context is never null in pop * changeset
1 parent 0cafe34 commit 8da222f

File tree

4 files changed

+30
-51
lines changed

4 files changed

+30
-51
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: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export function push(props, runes = false, fn) {
143143
p: component_context,
144144
c: null,
145145
e: null,
146-
m: false,
147146
s: props,
148147
x: null,
149148
l: null
@@ -171,37 +170,28 @@ export function push(props, runes = false, fn) {
171170
* @returns {T}
172171
*/
173172
export function pop(component) {
174-
const context_stack_item = component_context;
175-
if (context_stack_item !== null) {
176-
if (component !== undefined) {
177-
context_stack_item.x = component;
178-
}
179-
const component_effects = context_stack_item.e;
180-
if (component_effects !== null) {
181-
var previous_effect = active_effect;
182-
var previous_reaction = active_reaction;
183-
context_stack_item.e = null;
184-
try {
185-
for (var i = 0; i < component_effects.length; i++) {
186-
var component_effect = component_effects[i];
187-
set_active_effect(component_effect.effect);
188-
set_active_reaction(component_effect.reaction);
189-
create_user_effect(component_effect.fn);
190-
}
191-
} finally {
192-
set_active_effect(previous_effect);
193-
set_active_reaction(previous_reaction);
194-
}
195-
}
196-
component_context = context_stack_item.p;
197-
if (DEV) {
198-
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);
199181
}
200-
context_stack_item.m = true;
201182
}
202-
// Micro-optimization: Don't set .a above to the empty object
203-
// so it can be garbage-collected when the return here is unused
204-
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} */ ({});
205195
}
206196

207197
/** @returns {boolean} */

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/types.d.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ export type ComponentContext = {
1515
/** context */
1616
c: null | Map<unknown, unknown>;
1717
/** deferred effects */
18-
e: null | Array<{
19-
fn: () => void | (() => void);
20-
effect: null | Effect;
21-
reaction: null | Reaction;
22-
}>;
23-
/** mounted */
24-
m: boolean;
18+
e: null | Array<() => void | (() => void)>;
2519
/**
2620
* props — needed for legacy mode lifecycle functions, and for `createEventDispatcher`
2721
* @deprecated remove in 6.0

0 commit comments

Comments
 (0)