@@ -28,47 +28,33 @@ function identity(fn) {
2828 return fn ;
2929}
3030
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-
4331/** @type {ProxyMetadata | null } */
4432var parent_metadata = null ;
4533
4634/**
4735 * @template T
4836 * @param {T } value
49- * @param {ValueOptions } [_options ]
37+ * @param {() => void } [onchange ]
5038 * @param {Source<T> } [prev] dev mode only
5139 * @returns {T }
5240 */
53- export function proxy ( value , _options , prev ) {
41+ export function proxy ( value , onchange , prev ) {
5442 // if non-proxyable, or is already a proxy, return `value`
5543 if ( typeof value !== 'object' || value === null ) {
5644 return value ;
5745 }
5846
59- var options = clone_options ( _options ) ;
60-
6147 if ( STATE_SYMBOL in value ) {
6248 // @ts -ignore
63- value [ PROXY_ONCHANGE_SYMBOL ] ( options ?. onchange ) ;
49+ value [ PROXY_ONCHANGE_SYMBOL ] ( onchange ) ;
6450 return value ;
6551 }
6652
67- if ( options ?. onchange ) {
53+ if ( onchange ) {
6854 // if there's an onchange we actually store that but override the value
6955 // 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 = ( ) => {
7258 for ( let onchange of onchanges ) {
7359 onchange ( ) ;
7460 }
@@ -116,10 +102,7 @@ export function proxy(value, _options, prev) {
116102 if ( is_proxied_array ) {
117103 // We need to create the length source eagerly to ensure that
118104 // 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 ) ) ;
123106 }
124107
125108 /** @type {ProxyMetadata } */
@@ -165,12 +148,12 @@ export function proxy(value, _options, prev) {
165148 var s = sources . get ( prop ) ;
166149
167150 if ( s === undefined ) {
168- s = with_parent ( ( ) => source ( descriptor . value , clone_options ( options ) , stack ) ) ;
151+ s = with_parent ( ( ) => source ( descriptor . value , onchange , stack ) ) ;
169152 sources . set ( prop , s ) ;
170153 } else {
171154 set (
172155 s ,
173- with_parent ( ( ) => proxy ( descriptor . value , options ) )
156+ with_parent ( ( ) => proxy ( descriptor . value , onchange ) )
174157 ) ;
175158 }
176159
@@ -184,7 +167,7 @@ export function proxy(value, _options, prev) {
184167 if ( prop in target ) {
185168 sources . set (
186169 prop ,
187- with_parent ( ( ) => source ( UNINITIALIZED , clone_options ( options ) , stack ) )
170+ with_parent ( ( ) => source ( UNINITIALIZED , onchange , stack ) )
188171 ) ;
189172 }
190173 } else {
@@ -201,7 +184,7 @@ export function proxy(value, _options, prev) {
201184 // when we delete a property if the source is a proxy we remove the current onchange from
202185 // the proxy `onchanges` so that it doesn't trigger it anymore
203186 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 ) ;
205188 }
206189 set ( s , UNINITIALIZED ) ;
207190 update_version ( version ) ;
@@ -227,18 +210,14 @@ export function proxy(value, _options, prev) {
227210 // we either add or remove the passed in value
228211 // to the onchanges array or we set every source onchange
229212 // 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 ) {
231214 onchanges ?. add ?. ( value ) ;
232- } else if ( options ?. onchange != null && value ) {
215+ } else if ( onchange != null && value ) {
233216 onchanges ?. delete ?. ( value ) ;
234217 } else {
235- options = {
236- onchange : value
237- } ;
218+ onchange = value ;
238219 for ( let [ , s ] of sources ) {
239- if ( s . o ) {
240- s . o . onchange = value ;
241- }
220+ s . o = value ;
242221 }
243222 }
244223 } ;
@@ -249,7 +228,7 @@ export function proxy(value, _options, prev) {
249228
250229 // create a source, but only if it's an own property and not a prototype property
251230 if ( s === undefined && ( ! exists || get_descriptor ( target , prop ) ?. writable ) ) {
252- let opt = clone_options ( options ) ;
231+ let opt = onchange ;
253232 s = with_parent ( ( ) =>
254233 source ( proxy ( exists ? target [ prop ] : UNINITIALIZED , opt ) , opt , stack )
255234 ) ;
@@ -281,7 +260,7 @@ export function proxy(value, _options, prev) {
281260
282261 if (
283262 is_proxied_array &&
284- options ?. onchange != null &&
263+ onchange != null &&
285264 array_methods . includes ( /** @type {string } */ ( prop ) )
286265 ) {
287266 return batch_onchange ( v ) ;
@@ -330,7 +309,7 @@ export function proxy(value, _options, prev) {
330309 ( active_effect !== null && ( ! has || get_descriptor ( target , prop ) ?. writable ) )
331310 ) {
332311 if ( s === undefined ) {
333- let opt = clone_options ( options ) ;
312+ let opt = onchange ;
334313 s = with_parent ( ( ) => source ( has ? proxy ( target [ prop ] , opt ) : UNINITIALIZED , opt , stack ) ) ;
335314 sources . set ( prop , s ) ;
336315 }
@@ -362,14 +341,14 @@ export function proxy(value, _options, prev) {
362341 other_s . v !== null &&
363342 STATE_SYMBOL in other_s . v
364343 ) {
365- other_s . v [ PROXY_ONCHANGE_SYMBOL ] ( options ?. onchange , true ) ;
344+ other_s . v [ PROXY_ONCHANGE_SYMBOL ] ( onchange , true ) ;
366345 }
367346 set ( other_s , UNINITIALIZED ) ;
368347 } else if ( i in target ) {
369348 // If the item exists in the original, we need to create a uninitialized source,
370349 // else a later read of the property would result in a source being created with
371350 // 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 ) ) ;
373352 sources . set ( i + '' , other_s ) ;
374353 }
375354 }
@@ -381,7 +360,7 @@ export function proxy(value, _options, prev) {
381360 // object property before writing to that property.
382361 if ( s === undefined ) {
383362 if ( ! has || get_descriptor ( target , prop ) ?. writable ) {
384- const opt = clone_options ( options ) ;
363+ const opt = onchange ;
385364 s = with_parent ( ( ) => source ( undefined , opt , stack ) ) ;
386365 set (
387366 s ,
@@ -394,11 +373,11 @@ export function proxy(value, _options, prev) {
394373 // when we set a property if the source is a proxy we remove the current onchange from
395374 // the proxy `onchanges` so that it doesn't trigger it anymore
396375 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 ) ;
398377 }
399378 set (
400379 s ,
401- with_parent ( ( ) => proxy ( value , clone_options ( options ) ) )
380+ with_parent ( ( ) => proxy ( value , onchange ) )
402381 ) ;
403382 }
404383 } ) ( ) ;
@@ -464,11 +443,11 @@ export function proxy(value, _options, prev) {
464443/**
465444 * @template T
466445 * @param {T } value
467- * @param {ValueOptions } [options ]
446+ * @param {() => void } [onchange ]
468447 * @returns {Source<T> }
469448 */
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 ) ;
472451}
473452
474453/**
0 commit comments