1
- import { key , comment } from './render.js' ;
1
+ import { key } from './render.js' ;
2
2
import { source , set , get } from './runtime.js' ;
3
3
import { current_hydration_fragment } from './hydration.js' ;
4
4
import { child_frag } from './operations.js' ;
5
+ import { proxy } from './proxy/proxy.js' ;
5
6
6
7
function find_surrounding_ssr_commments ( ) {
7
8
if ( ! current_hydration_fragment ?. [ 0 ] ) return null ;
@@ -61,8 +62,6 @@ export function hmr(hot_data, new_component) {
61
62
62
63
// @ts -ignore
63
64
hot_data . proxy = function ( $$anchor , ...args ) {
64
- let accessors = /** @type {ComponentReturn } */ ( { } ) ;
65
-
66
65
// During hydration the root component will receive a null $$anchor. The
67
66
// following is a hack to get our `key` a node to render to, all while
68
67
// avoiding it to "consume" the SSR marker.
@@ -77,29 +76,36 @@ export function hmr(hot_data, new_component) {
77
76
}
78
77
}
79
78
79
+ const accessors_proxy = proxy ( /** @type {import('./proxy/proxy.js').StateObject } */ ( { } ) ) ;
80
+ /** @type {Set<string> } */
81
+ const accessors_keys = new Set ( ) ;
82
+
80
83
key (
81
84
$$anchor ,
82
85
( ) => get ( component_signal ) ,
83
86
( $$anchor ) => {
84
87
const component = get ( component_signal ) ;
85
88
// @ts -ignore
86
- accessors = component ( $$anchor , ...args ) ;
87
- }
88
- ) ;
89
+ const new_accessors = component ( $$anchor , ...args ) ;
89
90
90
- return new Proxy (
91
- { } ,
92
- {
93
- get ( _ , p ) {
94
- return accessors ?. [ p ] ;
95
- } ,
96
- set ( _ , p , value ) {
97
- // @ts -ignore (we actually want to crash on undefined, like non HMR code would do)
98
- accessors [ p ] = value ;
99
- return true ;
91
+ const removed_keys = new Set ( accessors_keys ) ;
92
+
93
+ if ( new_accessors ) {
94
+ for ( const [ key , value ] of Object . entries ( new_accessors ) ) {
95
+ accessors_proxy [ key ] = value ;
96
+ accessors_keys . add ( key ) ;
97
+ removed_keys . delete ( key ) ;
98
+ }
99
+ }
100
+
101
+ for ( const key of removed_keys ) {
102
+ accessors_keys . delete ( key ) ;
103
+ accessors_proxy [ key ] = undefined ;
100
104
}
101
105
}
102
106
) ;
107
+
108
+ return accessors_proxy ;
103
109
} ;
104
110
}
105
111
0 commit comments