@@ -11,8 +11,9 @@ import {
1111 set_active_reaction ,
1212 untrack
1313} from './runtime.js' ;
14- import { effect } from './reactivity/effects.js' ;
14+ import { effect , teardown } from './reactivity/effects.js' ;
1515import { legacy_mode_flag } from '../flags/index.js' ;
16+ import { CTX_CONTAINS_TEARDOWN , CTX_DESTROYED } from './constants.js' ;
1617
1718/** @type {ComponentContext | null } */
1819export let component_context = null ;
@@ -112,15 +113,17 @@ export function getAllContexts() {
112113 * @returns {void }
113114 */
114115export function push ( props , runes = false , fn ) {
115- component_context = {
116+ var ctx = ( component_context = {
116117 p : component_context ,
117118 c : null ,
118119 e : null ,
120+ f : 0 ,
119121 m : false ,
120122 s : props ,
121123 x : null ,
122- l : null
123- } ;
124+ l : null ,
125+ tp : props
126+ } ) ;
124127
125128 if ( legacy_mode_flag && ! runes ) {
126129 component_context . l = {
@@ -131,6 +134,24 @@ export function push(props, runes = false, fn) {
131134 } ;
132135 }
133136
137+ teardown ( ( ) => {
138+ if ( ctx . f !== CTX_CONTAINS_TEARDOWN ) {
139+ return ;
140+ }
141+ // Mark the context as destroyed, so any derived props can use
142+ // the latest known value before teardown
143+ ctx . f = CTX_DESTROYED ;
144+
145+ var teardown_props = ctx . tp ;
146+ // Apply the latest known props before teardown over existing props
147+ for ( var key in teardown_props ) {
148+ Object . defineProperty ( props , key , {
149+ value : teardown_props [ key ] ,
150+ configurable : true
151+ } ) ;
152+ }
153+ } ) ;
154+
134155 if ( DEV ) {
135156 // component function
136157 component_context . function = fn ;
@@ -171,6 +192,12 @@ export function pop(component) {
171192 dev_current_component_function = context_stack_item . p ?. function ?? null ;
172193 }
173194 context_stack_item . m = true ;
195+
196+ effect ( ( ) => {
197+ if ( context_stack_item . f === CTX_CONTAINS_TEARDOWN ) {
198+ context_stack_item . tp = { ...context_stack_item . s } ;
199+ }
200+ } ) ;
174201 }
175202 // Micro-optimization: Don't set .a above to the empty object
176203 // so it can be garbage-collected when the return here is unused
0 commit comments