Skip to content

Commit 3c6af50

Browse files
committed
tweak
1 parent a13b6dd commit 3c6af50

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from './runtime.js';
1414
import { effect, teardown } from './reactivity/effects.js';
1515
import { legacy_mode_flag } from '../flags/index.js';
16-
import { CTX_CONTAINS_TEARDOWN, CTX_DESTROYED, TEARDOWN_PROPS } from './constants.js';
16+
import { CTX_DESTROYED, TEARDOWN_PROPS } from './constants.js';
1717
import { define_property } from '../shared/utils.js';
1818

1919
/** @type {ComponentContext | null} */
@@ -136,24 +136,27 @@ export function push(props, runes = false, fn) {
136136
}
137137

138138
teardown(() => {
139-
if ((ctx.f & (CTX_CONTAINS_TEARDOWN | CTX_DESTROYED)) === 0) {
139+
if ((ctx.f & CTX_DESTROYED) !== 0) {
140140
return;
141141
}
142142
// Mark the context as destroyed, so any derived props can use
143143
// the latest known value before teardown
144144
ctx.f ^= CTX_DESTROYED;
145145

146-
var teardown_props = ctx.tp;
147-
if (TEARDOWN_PROPS in props) {
148-
props[TEARDOWN_PROPS] = teardown_props;
149-
return;
150-
}
151-
// Apply the latest known props before teardown over existing props
152-
for (var key in teardown_props) {
153-
define_property(props, key, {
154-
value: teardown_props[key],
155-
configurable: true
156-
});
146+
// Only apply the latest known props before teardown in legacy mode
147+
if (!is_runes()) {
148+
var teardown_props = ctx.tp;
149+
if (TEARDOWN_PROPS in props) {
150+
props[TEARDOWN_PROPS] = teardown_props;
151+
return;
152+
}
153+
// Apply the latest known props before teardown over existing props
154+
for (var key in teardown_props) {
155+
define_property(props, key, {
156+
value: teardown_props[key],
157+
configurable: true
158+
});
159+
}
157160
}
158161
});
159162

@@ -198,11 +201,12 @@ export function pop(component) {
198201
}
199202
context_stack_item.m = true;
200203

201-
effect(() => {
202-
if ((context_stack_item.f & CTX_CONTAINS_TEARDOWN) !== 0) {
204+
// Only apply the latest known props before teardown in legacy mode
205+
if (!is_runes()) {
206+
effect(() => {
203207
context_stack_item.tp = { ...context_stack_item.s };
204-
}
205-
});
208+
});
209+
}
206210
}
207211
// Micro-optimization: Don't set .a above to the empty object
208212
// so it can be garbage-collected when the return here is unused

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { Source } from './types.js' */
1+
/** @import { Derived, Source } from './types.js' */
22
import { DEV } from 'esm-env';
33
import {
44
PROPS_IS_BINDABLE,
@@ -250,6 +250,14 @@ export function spread_props(...props) {
250250
return new Proxy({ props }, spread_props_handler);
251251
}
252252

253+
/**
254+
* @param {Derived} signal
255+
* @returns {boolean}
256+
*/
257+
function in_destroyed_context(signal) {
258+
return signal.ctx !== null && (signal.ctx.f & CTX_DESTROYED) !== 0;
259+
}
260+
253261
/**
254262
* This function is responsible for synchronizing a possibly bound prop with the inner component state.
255263
* It is used whenever the compiler sees that the component writes to the prop, or when it has a default prop_value.
@@ -409,14 +417,17 @@ export function prop(props, key, flags, fallback) {
409417
if (fallback_used && fallback_value !== undefined) {
410418
fallback_value = new_value;
411419
}
420+
if (in_destroyed_context(current_value)) {
421+
return value;
422+
}
412423
untrack(() => get(current_value)); // force a synchronisation immediately
413424
}
414425

415426
return value;
416427
}
417428

418429
// If the prop is read, we might need to return the stale value if component ctx has been destroyed
419-
if (current_value.ctx !== null && (current_value.ctx.f & CTX_DESTROYED) !== 0) {
430+
if (in_destroyed_context(current_value)) {
420431
return current_value.v;
421432
}
422433

0 commit comments

Comments
 (0)