@ -2,6 +2,7 @@
import { DEV } from 'esm-env' ;
import { UNINITIALIZED } from '../../constants.js' ;
import { tracing _mode _flag } from '../flags/index.js' ;
import { get , active _effect , active _reaction , set _active _reaction } from './runtime.js' ;
import { component _context } from './context.js' ;
import {
array _prototype ,
@ -15,7 +16,6 @@ import { check_ownership, widen_ownership } from './dev/ownership.js';
import { get _stack } from './dev/tracing.js' ;
import * as e from './errors.js' ;
import { batch _onchange , set , source , state } from './reactivity/sources.js' ;
import { active _effect , get } from './runtime.js' ;
const array _methods = [ 'push' , 'pop' , 'shift' , 'unshift' , 'splice' , 'reverse' , 'sort' ] ;
@ -40,15 +40,17 @@ function clone_options(options) {
: undefined ;
}
/** @type {ProxyMetadata | null} */
var parent _metadata = null ;
/ * *
* @ template T
* @ param { T } value
* @ param { ValueOptions } [ _options ]
* @ param { ProxyMetadata | null } [ parent ]
* @ param { Source < T > } [ prev ] dev mode only
* @ returns { T }
* /
export function proxy ( value , _options , p arent = null , p rev) {
export function proxy ( value , _options , p rev) {
// if non-proxyable, or is already a proxy, return `value`
if ( typeof value !== 'object' || value === null ) {
return value ;
@ -85,6 +87,31 @@ export function proxy(value, _options, parent = null, prev) {
var version = source ( 0 ) ;
var stack = DEV && tracing _mode _flag ? get _stack ( 'CreatedAt' ) : null ;
var reaction = active _reaction ;
/ * *
* @ template T
* @ param { ( ) => T } fn
* /
var with _parent = ( fn ) => {
var previous _reaction = active _reaction ;
set _active _reaction ( reaction ) ;
/** @type {T} */
var result ;
if ( DEV ) {
var previous _metadata = parent _metadata ;
parent _metadata = metadata ;
result = fn ( ) ;
parent _metadata = previous _metadata ;
} else {
result = fn ( ) ;
}
set _active _reaction ( previous _reaction ) ;
return result ;
} ;
if ( is _proxied _array ) {
// We need to create the length source eagerly to ensure that
@ -100,7 +127,7 @@ export function proxy(value, _options, parent = null, prev) {
if ( DEV ) {
metadata = {
parent ,
parent : parent _metadata ,
owners : null
} ;
@ -112,7 +139,7 @@ export function proxy(value, _options, parent = null, prev) {
metadata . owners = prev _owners ? new Set ( prev _owners ) : null ;
} else {
metadata . owners =
parent === null
parent _metadata === null
? component _context !== null
? new Set ( [ component _context . function ] )
: null
@ -138,10 +165,13 @@ export function proxy(value, _options, parent = null, prev) {
var s = sources . get ( prop ) ;
if ( s === undefined ) {
s = source( descriptor . value , clone _options ( options ) , stack ) ;
s = with_parent ( ( ) => source( descriptor . value , clone _options ( options ) , stack ) ) ;
sources . set ( prop , s ) ;
} else {
set ( s , proxy ( descriptor . value , options , metadata ) ) ;
set (
s ,
with _parent ( ( ) => proxy ( descriptor . value , options ) )
) ;
}
return true ;
@ -152,7 +182,10 @@ export function proxy(value, _options, parent = null, prev) {
if ( s === undefined ) {
if ( prop in target ) {
sources . set ( prop , source ( UNINITIALIZED , clone _options ( options ) , stack ) ) ;
sources . set (
prop ,
with _parent ( ( ) => source ( UNINITIALIZED , clone _options ( options ) , stack ) )
) ;
}
} else {
// When working with arrays, we need to also ensure we update the length when removing
@ -217,7 +250,9 @@ export function proxy(value, _options, parent = null, prev) {
// create a source, but only if it's an own property and not a prototype property
if ( s === undefined && ( ! exists || get _descriptor ( target , prop ) ? . writable ) ) {
let opt = clone _options ( options ) ;
s = source ( proxy ( exists ? target [ prop ] : UNINITIALIZED , opt , metadata ) , opt , stack ) ;
s = with _parent ( ( ) =>
source ( proxy ( exists ? target [ prop ] : UNINITIALIZED , opt ) , opt , stack )
) ;
sources . set ( prop , s ) ;
}
@ -296,7 +331,7 @@ export function proxy(value, _options, parent = null, prev) {
) {
if ( s === undefined ) {
let opt = clone _options ( options ) ;
s = source( has ? proxy ( target [ prop ] , opt , metadata ) : UNINITIALIZED , opt , stack ) ;
s = with_parent ( ( ) => source( has ? proxy ( target [ prop ] , opt ) : UNINITIALIZED , opt , stack ) ) ;
sources . set ( prop , s ) ;
}
@ -334,7 +369,7 @@ export function proxy(value, _options, parent = null, prev) {
// If the item exists in the original, we need to create a uninitialized source,
// else a later read of the property would result in a source being created with
// the value of the original item at that index.
other _s = source( UNINITIALIZED , clone _options ( options ) , stack ) ;
other _s = with_parent ( ( ) => source( UNINITIALIZED , clone _options ( options ) , stack ) ) ;
sources . set ( i + '' , other _s ) ;
}
}
@ -347,8 +382,11 @@ export function proxy(value, _options, parent = null, prev) {
if ( s === undefined ) {
if ( ! has || get _descriptor ( target , prop ) ? . writable ) {
const opt = clone _options ( options ) ;
s = source ( undefined , opt , stack ) ;
set ( s , proxy ( value , opt , metadata ) ) ;
s = with _parent ( ( ) => source ( undefined , opt , stack ) ) ;
set (
s ,
with _parent ( ( ) => proxy ( value , opt ) )
) ;
sources . set ( prop , s ) ;
}
} else {
@ -358,7 +396,10 @@ export function proxy(value, _options, parent = null, prev) {
if ( typeof s . v === 'object' && s . v !== null && STATE _SYMBOL in s . v ) {
s . v [ PROXY _ONCHANGE _SYMBOL ] ( options ? . onchange , true ) ;
}
set ( s , proxy ( value , clone _options ( options ) , metadata ) ) ;
set (
s ,
with _parent ( ( ) => proxy ( value , clone _options ( options ) ) )
) ;
}
} ) ( ) ;
if ( DEV ) {