@@ -28,47 +28,33 @@ function identity(fn) {
28
28
return fn ;
29
29
}
30
30
31
- /**
32
- * @param {ValueOptions | undefined } options
33
- * @returns {ValueOptions | undefined }
34
- */
35
- function clone_options ( options ) {
36
- return options != null
37
- ? {
38
- onchange : options . onchange
39
- }
40
- : undefined ;
41
- }
42
-
43
31
/** @type {ProxyMetadata | null } */
44
32
var parent_metadata = null ;
45
33
46
34
/**
47
35
* @template T
48
36
* @param {T } value
49
- * @param {ValueOptions } [_options ]
37
+ * @param {() => void } [onchange ]
50
38
* @param {Source<T> } [prev] dev mode only
51
39
* @returns {T }
52
40
*/
53
- export function proxy ( value , _options , prev ) {
41
+ export function proxy ( value , onchange , prev ) {
54
42
// if non-proxyable, or is already a proxy, return `value`
55
43
if ( typeof value !== 'object' || value === null ) {
56
44
return value ;
57
45
}
58
46
59
- var options = clone_options ( _options ) ;
60
-
61
47
if ( STATE_SYMBOL in value ) {
62
48
// @ts -ignore
63
- value [ PROXY_ONCHANGE_SYMBOL ] ( options ?. onchange ) ;
49
+ value [ PROXY_ONCHANGE_SYMBOL ] ( onchange ) ;
64
50
return value ;
65
51
}
66
52
67
- if ( options ?. onchange ) {
53
+ if ( onchange ) {
68
54
// if there's an onchange we actually store that but override the value
69
55
// to store every other onchange that new proxies might add
70
- var onchanges = new Set ( [ options . onchange ] ) ;
71
- options . onchange = ( ) => {
56
+ var onchanges = new Set ( [ onchange ] ) ;
57
+ onchange = ( ) => {
72
58
for ( let onchange of onchanges ) {
73
59
onchange ( ) ;
74
60
}
@@ -116,10 +102,7 @@ export function proxy(value, _options, prev) {
116
102
if ( is_proxied_array ) {
117
103
// We need to create the length source eagerly to ensure that
118
104
// mutations to the array are properly synced with our proxy
119
- sources . set (
120
- 'length' ,
121
- source ( /** @type {any[] } */ ( value ) . length , clone_options ( options ) , stack )
122
- ) ;
105
+ sources . set ( 'length' , source ( /** @type {any[] } */ ( value ) . length , onchange , stack ) ) ;
123
106
}
124
107
125
108
/** @type {ProxyMetadata } */
@@ -165,12 +148,12 @@ export function proxy(value, _options, prev) {
165
148
var s = sources . get ( prop ) ;
166
149
167
150
if ( s === undefined ) {
168
- s = with_parent ( ( ) => source ( descriptor . value , clone_options ( options ) , stack ) ) ;
151
+ s = with_parent ( ( ) => source ( descriptor . value , onchange , stack ) ) ;
169
152
sources . set ( prop , s ) ;
170
153
} else {
171
154
set (
172
155
s ,
173
- with_parent ( ( ) => proxy ( descriptor . value , options ) )
156
+ with_parent ( ( ) => proxy ( descriptor . value , onchange ) )
174
157
) ;
175
158
}
176
159
@@ -184,7 +167,7 @@ export function proxy(value, _options, prev) {
184
167
if ( prop in target ) {
185
168
sources . set (
186
169
prop ,
187
- with_parent ( ( ) => source ( UNINITIALIZED , clone_options ( options ) , stack ) )
170
+ with_parent ( ( ) => source ( UNINITIALIZED , onchange , stack ) )
188
171
) ;
189
172
}
190
173
} else {
@@ -201,7 +184,7 @@ export function proxy(value, _options, prev) {
201
184
// when we delete a property if the source is a proxy we remove the current onchange from
202
185
// the proxy `onchanges` so that it doesn't trigger it anymore
203
186
if ( typeof s . v === 'object' && s . v !== null && STATE_SYMBOL in s . v ) {
204
- s . v [ PROXY_ONCHANGE_SYMBOL ] ( options ?. onchange , true ) ;
187
+ s . v [ PROXY_ONCHANGE_SYMBOL ] ( onchange , true ) ;
205
188
}
206
189
set ( s , UNINITIALIZED ) ;
207
190
update_version ( version ) ;
@@ -227,18 +210,14 @@ export function proxy(value, _options, prev) {
227
210
// we either add or remove the passed in value
228
211
// to the onchanges array or we set every source onchange
229
212
// to the passed in value (if it's undefined it will make the chain stop)
230
- if ( options ?. onchange != null && value && ! remove ) {
213
+ if ( onchange != null && value && ! remove ) {
231
214
onchanges ?. add ?. ( value ) ;
232
- } else if ( options ?. onchange != null && value ) {
215
+ } else if ( onchange != null && value ) {
233
216
onchanges ?. delete ?. ( value ) ;
234
217
} else {
235
- options = {
236
- onchange : value
237
- } ;
218
+ onchange = value ;
238
219
for ( let [ , s ] of sources ) {
239
- if ( s . o ) {
240
- s . o . onchange = value ;
241
- }
220
+ s . o = value ;
242
221
}
243
222
}
244
223
} ;
@@ -249,7 +228,7 @@ export function proxy(value, _options, prev) {
249
228
250
229
// create a source, but only if it's an own property and not a prototype property
251
230
if ( s === undefined && ( ! exists || get_descriptor ( target , prop ) ?. writable ) ) {
252
- let opt = clone_options ( options ) ;
231
+ let opt = onchange ;
253
232
s = with_parent ( ( ) =>
254
233
source ( proxy ( exists ? target [ prop ] : UNINITIALIZED , opt ) , opt , stack )
255
234
) ;
@@ -281,7 +260,7 @@ export function proxy(value, _options, prev) {
281
260
282
261
if (
283
262
is_proxied_array &&
284
- options ?. onchange != null &&
263
+ onchange != null &&
285
264
array_methods . includes ( /** @type {string } */ ( prop ) )
286
265
) {
287
266
return batch_onchange ( v ) ;
@@ -330,7 +309,7 @@ export function proxy(value, _options, prev) {
330
309
( active_effect !== null && ( ! has || get_descriptor ( target , prop ) ?. writable ) )
331
310
) {
332
311
if ( s === undefined ) {
333
- let opt = clone_options ( options ) ;
312
+ let opt = onchange ;
334
313
s = with_parent ( ( ) => source ( has ? proxy ( target [ prop ] , opt ) : UNINITIALIZED , opt , stack ) ) ;
335
314
sources . set ( prop , s ) ;
336
315
}
@@ -362,14 +341,14 @@ export function proxy(value, _options, prev) {
362
341
other_s . v !== null &&
363
342
STATE_SYMBOL in other_s . v
364
343
) {
365
- other_s . v [ PROXY_ONCHANGE_SYMBOL ] ( options ?. onchange , true ) ;
344
+ other_s . v [ PROXY_ONCHANGE_SYMBOL ] ( onchange , true ) ;
366
345
}
367
346
set ( other_s , UNINITIALIZED ) ;
368
347
} else if ( i in target ) {
369
348
// If the item exists in the original, we need to create a uninitialized source,
370
349
// else a later read of the property would result in a source being created with
371
350
// the value of the original item at that index.
372
- other_s = with_parent ( ( ) => source ( UNINITIALIZED , clone_options ( options ) , stack ) ) ;
351
+ other_s = with_parent ( ( ) => source ( UNINITIALIZED , onchange , stack ) ) ;
373
352
sources . set ( i + '' , other_s ) ;
374
353
}
375
354
}
@@ -381,7 +360,7 @@ export function proxy(value, _options, prev) {
381
360
// object property before writing to that property.
382
361
if ( s === undefined ) {
383
362
if ( ! has || get_descriptor ( target , prop ) ?. writable ) {
384
- const opt = clone_options ( options ) ;
363
+ const opt = onchange ;
385
364
s = with_parent ( ( ) => source ( undefined , opt , stack ) ) ;
386
365
set (
387
366
s ,
@@ -394,11 +373,11 @@ export function proxy(value, _options, prev) {
394
373
// when we set a property if the source is a proxy we remove the current onchange from
395
374
// the proxy `onchanges` so that it doesn't trigger it anymore
396
375
if ( typeof s . v === 'object' && s . v !== null && STATE_SYMBOL in s . v ) {
397
- s . v [ PROXY_ONCHANGE_SYMBOL ] ( options ?. onchange , true ) ;
376
+ s . v [ PROXY_ONCHANGE_SYMBOL ] ( onchange , true ) ;
398
377
}
399
378
set (
400
379
s ,
401
- with_parent ( ( ) => proxy ( value , clone_options ( options ) ) )
380
+ with_parent ( ( ) => proxy ( value , onchange ) )
402
381
) ;
403
382
}
404
383
} ) ( ) ;
@@ -464,11 +443,11 @@ export function proxy(value, _options, prev) {
464
443
/**
465
444
* @template T
466
445
* @param {T } value
467
- * @param {ValueOptions } [options ]
446
+ * @param {() => void } [onchange ]
468
447
* @returns {Source<T> }
469
448
*/
470
- export function assignable_proxy ( value , options ) {
471
- return state ( proxy ( value , options ) , options ) ;
449
+ export function assignable_proxy ( value , onchange ) {
450
+ return state ( proxy ( value , onchange ) , onchange ) ;
472
451
}
473
452
474
453
/**
0 commit comments