@@ -127,11 +127,11 @@ const buildProxyFunction = (
127
127
128
128
versionHolder = [ 1 , 1 ] as [ number , number ] ,
129
129
130
- proxyFunction = < T extends object > ( initialObject : T ) : T => {
131
- if ( ! isObject ( initialObject ) ) {
130
+ proxyFunction = < T extends object > ( baseObject : T ) : T => {
131
+ if ( ! isObject ( baseObject ) ) {
132
132
throw new Error ( 'object required' )
133
133
}
134
- const found = proxyCache . get ( initialObject ) as T | undefined
134
+ const found = proxyCache . get ( baseObject ) as T | undefined
135
135
if ( found ) {
136
136
return found
137
137
}
@@ -167,18 +167,23 @@ const buildProxyFunction = (
167
167
string | symbol ,
168
168
readonly [ ProxyState , RemoveListener ?]
169
169
> ( )
170
- const addPropListener = (
171
- prop : string | symbol ,
172
- propProxyState : ProxyState
173
- ) => {
174
- if ( import . meta. env ?. MODE !== 'production' && propProxyStates . has ( prop ) ) {
175
- throw new Error ( 'prop listener already exists' )
176
- }
177
- if ( listeners . size ) {
178
- const remove = propProxyState [ 3 ] ( createPropListener ( prop ) )
179
- propProxyStates . set ( prop , [ propProxyState , remove ] )
180
- } else {
181
- propProxyStates . set ( prop , [ propProxyState ] )
170
+ const addPropListener = ( prop : string | symbol , propValue : unknown ) => {
171
+ const propProxyState =
172
+ ! refSet . has ( propValue as object ) &&
173
+ proxyStateMap . get ( propValue as object )
174
+ if ( propProxyState ) {
175
+ if (
176
+ import . meta. env ?. MODE !== 'production' &&
177
+ propProxyStates . has ( prop )
178
+ ) {
179
+ throw new Error ( 'prop listener already exists' )
180
+ }
181
+ if ( listeners . size ) {
182
+ const remove = propProxyState [ 3 ] ( createPropListener ( prop ) )
183
+ propProxyStates . set ( prop , [ propProxyState , remove ] )
184
+ } else {
185
+ propProxyStates . set ( prop , [ propProxyState ] )
186
+ }
182
187
}
183
188
}
184
189
const removePropListener = ( prop : string | symbol ) => {
@@ -212,9 +217,7 @@ const buildProxyFunction = (
212
217
}
213
218
return removeListener
214
219
}
215
- const baseObject = Array . isArray ( initialObject )
216
- ? [ ]
217
- : Object . create ( Object . getPrototypeOf ( initialObject ) )
220
+ let initializing = true
218
221
const handler : ProxyHandler < T > = {
219
222
deleteProperty ( target : T , prop : string | symbol ) {
220
223
const prevValue = Reflect . get ( target , prop )
@@ -226,7 +229,7 @@ const buildProxyFunction = (
226
229
return deleted
227
230
} ,
228
231
set ( target : T , prop : string | symbol , value : any , receiver : object ) {
229
- const hasPrevValue = Reflect . has ( target , prop )
232
+ const hasPrevValue = ! initializing && Reflect . has ( target , prop )
230
233
const prevValue = Reflect . get ( target , prop , receiver )
231
234
if (
232
235
hasPrevValue &&
@@ -257,40 +260,32 @@ const buildProxyFunction = (
257
260
if ( ! proxyStateMap . has ( value ) && canProxy ( value ) ) {
258
261
nextValue = proxyFunction ( value )
259
262
}
260
- const childProxyState =
261
- ! refSet . has ( nextValue ) && proxyStateMap . get ( nextValue )
262
- if ( childProxyState ) {
263
- addPropListener ( prop , childProxyState )
264
- }
263
+ addPropListener ( prop , nextValue )
265
264
}
266
265
Reflect . set ( target , prop , nextValue , receiver )
267
266
notifyUpdate ( [ 'set' , [ prop ] , value , prevValue ] )
268
267
return true
269
268
} ,
270
269
}
271
270
const proxyObject = newProxy ( baseObject , handler )
272
- proxyCache . set ( initialObject , proxyObject )
271
+ proxyCache . set ( baseObject , proxyObject )
273
272
const proxyState : ProxyState = [
274
273
baseObject ,
275
274
ensureVersion ,
276
275
createSnapshot ,
277
276
addListener ,
278
277
]
279
278
proxyStateMap . set ( proxyObject , proxyState )
280
- Reflect . ownKeys ( initialObject ) . forEach ( ( key ) => {
279
+ Reflect . ownKeys ( baseObject ) . forEach ( ( key ) => {
281
280
const desc = Object . getOwnPropertyDescriptor (
282
- initialObject ,
281
+ baseObject ,
283
282
key
284
283
) as PropertyDescriptor
285
- if ( 'value' in desc ) {
286
- proxyObject [ key as keyof T ] = initialObject [ key as keyof T ]
287
- // We need to delete desc.value because we already set it,
288
- // and delete desc.writable because we want to write it again.
289
- delete desc . value
290
- delete desc . writable
284
+ if ( 'value' in desc && desc . writable ) {
285
+ proxyObject [ key as keyof T ] = baseObject [ key as keyof T ]
291
286
}
292
- Object . defineProperty ( baseObject , key , desc )
293
287
} )
288
+ initializing = false
294
289
return proxyObject
295
290
}
296
291
) =>
@@ -312,8 +307,8 @@ const buildProxyFunction = (
312
307
313
308
const [ defaultProxyFunction ] = buildProxyFunction ( )
314
309
315
- export function proxy < T extends object > ( initialObject : T = { } as T ) : T {
316
- return defaultProxyFunction ( initialObject )
310
+ export function proxy < T extends object > ( baseObject : T = { } as T ) : T {
311
+ return defaultProxyFunction ( baseObject )
317
312
}
318
313
319
314
export function getVersion ( proxyObject : unknown ) : number | undefined {
0 commit comments