@ -1,7 +1,8 @@
import { DEV } from 'esm-env' ;
import { subscribe _to _store } from '../../store/utils.js' ;
import { EMPTY _FUNC , run _all } from '../common.js' ;
import { get _descriptors , is _array } from './utils.js' ;
import { get _descriptor , get _descriptors , is _array } from './utils.js' ;
import { PROPS _CALL _DEFAULT _VALUE , PROPS _IS _IMMUTABLE } from '../../constants.js' ;
export const SOURCE = 1 ;
export const DERIVED = 1 << 1 ;
@ -30,8 +31,7 @@ let current_scheduler_mode = FLUSH_MICROTASK;
// Used for handling scheduling
let is _micro _task _queued = false ;
let is _task _queued = false ;
// Used for exposing signals
let is _signal _exposed = false ;
// Handle effect queues
/** @type {import('./types.js').EffectSignal[]} */
@ -63,8 +63,6 @@ export let current_untracking = false;
/** Exists to opt out of the mutation validation for stores which may be set for the first time during a derivation */
let ignore _mutation _validation = false ;
/** @type {null | import('./types.js').Signal} */
let current _captured _signal = null ;
// If we are working with a get() chain that has no active container,
// to prevent memory leaks, we skip adding the consumer.
let current _skip _consumer = false ;
@ -800,23 +798,6 @@ export function unsubscribe_on_destroy(stores) {
} ) ;
}
/ * *
* Wraps a function and marks execution context so that the last signal read from can be captured
* using the ` expose ` function .
* @ template V
* @ param { ( ) => V } fn
* @ returns { V }
* /
export function exposable ( fn ) {
const previous _is _signal _exposed = is _signal _exposed ;
try {
is _signal _exposed = true ;
return fn ( ) ;
} finally {
is _signal _exposed = previous _is _signal _exposed ;
}
}
/ * *
* @ template V
* @ param { import ( './types.js' ) . Signal < V > } signal
@ -836,10 +817,6 @@ export function get(signal) {
return signal . v ;
}
if ( is _signal _exposed && current _should _capture _signal ) {
current _captured _signal = signal ;
}
if ( is _signals _recorded ) {
captured _signals . add ( signal ) ;
}
@ -906,31 +883,6 @@ export function set_sync(signal, value) {
flushSync ( ( ) => set ( signal , value ) ) ;
}
/ * *
* Invokes a function and captures the last signal that is read during the invocation
* if that signal is read within the ` exposable ` function context .
* If a signal is captured , it returns the signal instead of the read value .
* @ template V
* @ param { ( ) => V } possible _signal _fn
* @ returns { any }
* /
export function expose ( possible _signal _fn ) {
const previous _captured _signal = current _captured _signal ;
const previous _should _capture _signal = current _should _capture _signal ;
current _captured _signal = null ;
current _should _capture _signal = true ;
try {
const value = possible _signal _fn ( ) ;
if ( current _captured _signal === null ) {
return value ;
}
return current _captured _signal ;
} finally {
current _captured _signal = previous _captured _signal ;
current _should _capture _signal = previous _should _capture _signal ;
}
}
/ * *
* Invokes a function and captures all signals that are read during the invocation ,
* then invalidates them .
@ -1463,35 +1415,19 @@ export function is_store(val) {
* @ template V
* @ param { import ( './types.js' ) . MaybeSignal < Record < string , unknown >> } props _obj
* @ param { string } key
* @ param { boolean } immutable
* @ param { number } flags
* @ param { V | ( ( ) => V ) } [ default _value ]
* @ param { boolean } [ call _default _value ]
* @ returns { import ( './types.js' ) . Signal < V > | ( ( ) => V ) }
* /
export function prop _source ( props _obj , key , immutable , default _value , call _default _value ) {
export function prop _source ( props _obj , key , flags , default _value ) {
const call _default _value = ( flags & PROPS _CALL _DEFAULT _VALUE ) !== 0 ;
const immutable = ( flags & PROPS _IS _IMMUTABLE ) !== 0 ;
const props = is _signal ( props _obj ) ? get ( props _obj ) : props _obj ;
const possible _signal = /** @type {import('./types.js').MaybeSignal<V>} */ (
expose ( ( ) => props [ key ] )
) ;
const update _bound _prop = Object . getOwnPropertyDescriptor ( props , key ) ? . set ;
const update _bound _prop = get _descriptor ( props , key ) ? . set ;
let value = props [ key ] ;
const should _set _default _value = value === undefined && default _value !== undefined ;
if (
is _signal ( possible _signal ) &&
possible _signal . v === value &&
update _bound _prop === undefined
) {
if ( should _set _default _value ) {
set (
possible _signal ,
// @ts-expect-error would need a cumbersome method overload to type this
call _default _value ? default _value ( ) : default _value
) ;
}
return possible _signal ;
}
if ( should _set _default _value ) {
value =
// @ts-expect-error would need a cumbersome method overload to type this
@ -1534,7 +1470,7 @@ export function prop_source(props_obj, key, immutable, default_value, call_defau
}
} ) ;
if ( is_signal ( possible _signal ) && update_bound _prop !== undefined ) {
if ( update_bound _prop !== undefined ) {
let ignore _first = ! should _set _default _value ;
sync _effect ( ( ) => {
// Before if to ensure signal dependency is registered
@ -1548,11 +1484,9 @@ export function prop_source(props_obj, key, immutable, default_value, call_defau
return ;
}
if ( not _equal ( immutable , propagating _value , possible _signal . v ) ) {
ignore _next1 = true ;
did _update _to _defined = true ;
untrack ( ( ) => update _bound _prop ( propagating _value ) ) ;
}
ignore _next1 = true ;
did _update _to _defined = true ;
untrack ( ( ) => update _bound _prop ( propagating _value ) ) ;
} ) ;
}