only add source.o to user-created sources

state-onchange-roots
Rich Harris 5 months ago
parent ac05b73380
commit 356ce3d28d

@ -90,7 +90,7 @@ export function proxy(value, onchange) {
if (is_proxied_array) { if (is_proxied_array) {
// We need to create the length source eagerly to ensure that // We need to create the length source eagerly to ensure that
// mutations to the array are properly synced with our proxy // mutations to the array are properly synced with our proxy
sources.set('length', source(/** @type {any[]} */ (value).length, onchange, stack)); sources.set('length', source(/** @type {any[]} */ (value).length, stack));
} }
return new Proxy(/** @type {any} */ (value), { return new Proxy(/** @type {any} */ (value), {
@ -111,7 +111,7 @@ export function proxy(value, onchange) {
var s = sources.get(prop); var s = sources.get(prop);
if (s === undefined) { if (s === undefined) {
s = with_parent(() => source(descriptor.value, onchange, stack)); s = with_parent(() => source(descriptor.value, stack));
sources.set(prop, s); sources.set(prop, s);
} else { } else {
set( set(
@ -130,7 +130,7 @@ export function proxy(value, onchange) {
if (prop in target) { if (prop in target) {
sources.set( sources.set(
prop, prop,
with_parent(() => source(UNINITIALIZED, onchange, stack)) with_parent(() => source(UNINITIALIZED, stack))
); );
} }
} else { } else {
@ -182,9 +182,8 @@ export function proxy(value, onchange) {
// create a source, but only if it's an own property and not a prototype property // 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)) { if (s === undefined && (!exists || get_descriptor(target, prop)?.writable)) {
let opt = onchange;
s = with_parent(() => s = with_parent(() =>
source(proxy(exists ? target[prop] : UNINITIALIZED, opt), opt, stack) source(proxy(exists ? target[prop] : UNINITIALIZED, onchange), stack)
); );
sources.set(prop, s); sources.set(prop, s);
} }
@ -243,8 +242,7 @@ export function proxy(value, onchange) {
(active_effect !== null && (!has || get_descriptor(target, prop)?.writable)) (active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
) { ) {
if (s === undefined) { if (s === undefined) {
let opt = onchange; s = with_parent(() => source(has ? proxy(target[prop], onchange) : UNINITIALIZED, stack));
s = with_parent(() => source(has ? proxy(target[prop], opt) : UNINITIALIZED, opt, stack));
sources.set(prop, s); sources.set(prop, s);
} }
@ -283,7 +281,7 @@ export function proxy(value, onchange) {
// If the item exists in the original, we need to create a uninitialized source, // 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 // else a later read of the property would result in a source being created with
// the value of the original item at that index. // the value of the original item at that index.
other_s = with_parent(() => source(UNINITIALIZED, onchange, stack)); other_s = with_parent(() => source(UNINITIALIZED, stack));
sources.set(i + '', other_s); sources.set(i + '', other_s);
} }
} }
@ -295,7 +293,7 @@ export function proxy(value, onchange) {
// object property before writing to that property. // object property before writing to that property.
if (s === undefined) { if (s === undefined) {
if (!has || get_descriptor(target, prop)?.writable) { if (!has || get_descriptor(target, prop)?.writable) {
s = with_parent(() => source(undefined, onchange, stack)); s = with_parent(() => source(undefined, stack));
sources.set(prop, s); sources.set(prop, s);
} }
} else { } else {

@ -76,12 +76,11 @@ export function batch_onchange(fn) {
/** /**
* @template V * @template V
* @param {V} v * @param {V} v
* @param {() => void} [o]
* @param {Error | null} [stack] * @param {Error | null} [stack]
* @returns {Source<V>} * @returns {Source<V>}
*/ */
// TODO rename this to `state` throughout the codebase // TODO rename this to `state` throughout the codebase
export function source(v, o, stack) { export function source(v, stack) {
/** @type {Value} */ /** @type {Value} */
var signal = { var signal = {
f: 0, // TODO ideally we could skip this altogether, but it causes type errors f: 0, // TODO ideally we could skip this altogether, but it causes type errors
@ -89,8 +88,7 @@ export function source(v, o, stack) {
reactions: null, reactions: null,
equals, equals,
rv: 0, rv: 0,
wv: 0, wv: 0
o
}; };
if (DEV && tracing_mode_flag) { if (DEV && tracing_mode_flag) {
@ -109,7 +107,8 @@ export function source(v, o, stack) {
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function state(v, o, stack) { export function state(v, o, stack) {
const s = source(v, o, stack); const s = source(v, stack);
if (o) s.o = o;
push_reaction_value(s); push_reaction_value(s);

Loading…
Cancel
Save