@@ -76,9 +76,10 @@ const publicPropertiesMap: Record<
76
76
}
77
77
78
78
const enum AccessTypes {
79
+ SETUP ,
79
80
DATA ,
80
- CONTEXT ,
81
81
PROPS ,
82
+ CONTEXT ,
82
83
OTHER
83
84
}
84
85
@@ -91,6 +92,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
91
92
get ( { _ : instance } : ComponentPublicProxyTarget , key : string ) {
92
93
const {
93
94
renderContext,
95
+ setupState,
94
96
data,
95
97
props,
96
98
accessCache,
@@ -109,6 +111,8 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
109
111
const n = accessCache ! [ key ]
110
112
if ( n !== undefined ) {
111
113
switch ( n ) {
114
+ case AccessTypes . SETUP :
115
+ return setupState [ key ]
112
116
case AccessTypes . DATA :
113
117
return data [ key ]
114
118
case AccessTypes . CONTEXT :
@@ -117,22 +121,25 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
117
121
return props ! [ key ]
118
122
// default: just fallthrough
119
123
}
124
+ } else if ( setupState !== EMPTY_OBJ && hasOwn ( setupState , key ) ) {
125
+ accessCache ! [ key ] = AccessTypes . SETUP
126
+ return setupState [ key ]
120
127
} else if ( data !== EMPTY_OBJ && hasOwn ( data , key ) ) {
121
128
accessCache ! [ key ] = AccessTypes . DATA
122
129
return data [ key ]
130
+ } else if (
131
+ // only cache other properties when instance has declared (thus stable)
132
+ // props
133
+ type . props &&
134
+ hasOwn ( normalizePropsOptions ( type . props ) [ 0 ] ! , key )
135
+ ) {
136
+ accessCache ! [ key ] = AccessTypes . PROPS
137
+ return props ! [ key ]
123
138
} else if ( renderContext !== EMPTY_OBJ && hasOwn ( renderContext , key ) ) {
124
139
accessCache ! [ key ] = AccessTypes . CONTEXT
125
140
return renderContext [ key ]
126
- } else if ( type . props ) {
127
- // only cache other properties when instance has declared (thus stable)
128
- // props
129
- if ( hasOwn ( normalizePropsOptions ( type . props ) [ 0 ] ! , key ) ) {
130
- accessCache ! [ key ] = AccessTypes . PROPS
131
- // return the value from propsProxy for ref unwrapping and readonly
132
- return props ! [ key ]
133
- } else {
134
- accessCache ! [ key ] = AccessTypes . OTHER
135
- }
141
+ } else {
142
+ accessCache ! [ key ] = AccessTypes . OTHER
136
143
}
137
144
}
138
145
@@ -170,23 +177,25 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
170
177
key : string ,
171
178
value : any
172
179
) : boolean {
173
- const { data, renderContext } = instance
174
- if ( data !== EMPTY_OBJ && hasOwn ( data , key ) ) {
180
+ const { data, setupState, renderContext } = instance
181
+ if ( setupState !== EMPTY_OBJ && hasOwn ( setupState , key ) ) {
182
+ setupState [ key ] = value
183
+ } else if ( data !== EMPTY_OBJ && hasOwn ( data , key ) ) {
175
184
data [ key ] = value
176
- } else if ( hasOwn ( renderContext , key ) ) {
177
- renderContext [ key ] = value
178
- } else if ( key [ 0 ] === '$' && key . slice ( 1 ) in instance ) {
185
+ } else if ( key in instance . props ) {
179
186
__DEV__ &&
180
187
warn (
181
- `Attempting to mutate public property "${ key } ". ` +
182
- `Properties starting with $ are reserved and readonly.` ,
188
+ `Attempting to mutate prop "${ key } ". Props are readonly.` ,
183
189
instance
184
190
)
185
191
return false
186
- } else if ( key in instance . props ) {
192
+ } else if ( hasOwn ( renderContext , key ) ) {
193
+ renderContext [ key ] = value
194
+ } else if ( key [ 0 ] === '$' && key . slice ( 1 ) in instance ) {
187
195
__DEV__ &&
188
196
warn (
189
- `Attempting to mutate prop "${ key } ". Props are readonly.` ,
197
+ `Attempting to mutate public property "${ key } ". ` +
198
+ `Properties starting with $ are reserved and readonly.` ,
190
199
instance
191
200
)
192
201
return false
@@ -206,15 +215,24 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
206
215
207
216
has (
208
217
{
209
- _ : { data, accessCache, renderContext, type, proxyTarget, appContext }
218
+ _ : {
219
+ data,
220
+ setupState,
221
+ accessCache,
222
+ renderContext,
223
+ type,
224
+ proxyTarget,
225
+ appContext
226
+ }
210
227
} : ComponentPublicProxyTarget ,
211
228
key : string
212
229
) {
213
230
return (
214
231
accessCache ! [ key ] !== undefined ||
215
232
( data !== EMPTY_OBJ && hasOwn ( data , key ) ) ||
216
- hasOwn ( renderContext , key ) ||
233
+ ( setupState !== EMPTY_OBJ && hasOwn ( setupState , key ) ) ||
217
234
( type . props && hasOwn ( normalizePropsOptions ( type . props ) [ 0 ] ! , key ) ) ||
235
+ hasOwn ( renderContext , key ) ||
218
236
hasOwn ( publicPropertiesMap , key ) ||
219
237
hasOwn ( proxyTarget , key ) ||
220
238
hasOwn ( appContext . config . globalProperties , key )
@@ -306,15 +324,15 @@ export function exposePropsOnDevProxyTarget(
306
324
}
307
325
}
308
326
309
- export function exposeRenderContextOnDevProxyTarget (
327
+ export function exposeSetupStateOnDevProxyTarget (
310
328
instance : ComponentInternalInstance
311
329
) {
312
- const { proxyTarget, renderContext } = instance
313
- Object . keys ( toRaw ( renderContext ) ) . forEach ( key => {
330
+ const { proxyTarget, setupState } = instance
331
+ Object . keys ( toRaw ( setupState ) ) . forEach ( key => {
314
332
Object . defineProperty ( proxyTarget , key , {
315
333
enumerable : true ,
316
334
configurable : true ,
317
- get : ( ) => renderContext [ key ] ,
335
+ get : ( ) => setupState [ key ] ,
318
336
set : NOOP
319
337
} )
320
338
} )
0 commit comments