@@ -6,7 +6,7 @@ import { render_effect, user_pre_effect } from '../reactivity/effects.js';
6
6
import { dev_current_component_function } from '../context.js' ;
7
7
import { get_prototype_of } from '../../shared/utils.js' ;
8
8
import * as w from '../warnings.js' ;
9
- import { FILENAME } from '../../../constants.js' ;
9
+ import { FILENAME , UNINITIALIZED } from '../../../constants.js' ;
10
10
11
11
/** @type {Record<string, Array<{ start: Location, end: Location, component: Function }>> } */
12
12
const boundaries = { } ;
@@ -140,6 +140,25 @@ export function add_owner_effect(get_object, Component, skip_warning = false) {
140
140
} ) ;
141
141
}
142
142
143
+ /**
144
+ * @param {any } _this
145
+ * @param {Function } owner
146
+ * @param {Array<() => any> } getters
147
+ * @param {boolean } skip_warning
148
+ */
149
+ export function add_owner_to_class ( _this , owner , getters , skip_warning ) {
150
+ _this [ ADD_OWNER ] . current ||= getters . map ( ( ) => UNINITIALIZED ) ;
151
+
152
+ for ( let i = 0 ; i < getters . length ; i += 1 ) {
153
+ const current = getters [ i ] ( ) ;
154
+ // For performance reasons we only re-add the owner if the state has changed
155
+ if ( current !== _this [ ADD_OWNER ] [ i ] ) {
156
+ _this [ ADD_OWNER ] . current [ i ] = current ;
157
+ add_owner ( current , owner , false , skip_warning ) ;
158
+ }
159
+ }
160
+ }
161
+
143
162
/**
144
163
* @param {ProxyMetadata | null } from
145
164
* @param {ProxyMetadata } to
@@ -196,7 +215,19 @@ function add_owner_to_object(object, owner, seen) {
196
215
if ( proto === Object . prototype ) {
197
216
// recurse until we find a state proxy
198
217
for ( const key in object ) {
199
- add_owner_to_object ( object [ key ] , owner , seen ) ;
218
+ if ( Object . getOwnPropertyDescriptor ( object , key ) ?. get ) {
219
+ // Similar to the class case; the getter could update with a new state
220
+ let current = UNINITIALIZED ;
221
+ render_effect ( ( ) => {
222
+ const next = object [ key ] ;
223
+ if ( current !== next ) {
224
+ current = next ;
225
+ add_owner_to_object ( next , owner , seen ) ;
226
+ }
227
+ } ) ;
228
+ } else {
229
+ add_owner_to_object ( object [ key ] , owner , seen ) ;
230
+ }
200
231
}
201
232
} else if ( proto === Array . prototype ) {
202
233
// recurse until we find a state proxy
@@ -221,9 +252,10 @@ function has_owner(metadata, component) {
221
252
return (
222
253
metadata . owners . has ( component ) ||
223
254
// This helps avoid false positives when using HMR, where the component function is replaced
224
- [ ...metadata . owners ] . some (
225
- ( owner ) => /** @type {any } */ ( owner ) [ FILENAME ] === /** @type {any } */ ( component ) ?. [ FILENAME ]
226
- ) ||
255
+ ( FILENAME in component &&
256
+ [ ...metadata . owners ] . some (
257
+ ( owner ) => /** @type {any } */ ( owner ) [ FILENAME ] === component [ FILENAME ]
258
+ ) ) ||
227
259
( metadata . parent !== null && has_owner ( metadata . parent , component ) )
228
260
) ;
229
261
}
0 commit comments