|
|
|
|
@ -34,18 +34,16 @@ var parent_metadata = null;
|
|
|
|
|
/**
|
|
|
|
|
* @template T
|
|
|
|
|
* @param {T} value
|
|
|
|
|
* @param {ValueOptions} [options]
|
|
|
|
|
* @param {() => void} [onchange]
|
|
|
|
|
* @param {Source<T>} [prev] dev mode only
|
|
|
|
|
* @returns {T}
|
|
|
|
|
*/
|
|
|
|
|
export function proxy(value, options, prev) {
|
|
|
|
|
export function proxy(value, onchange, prev) {
|
|
|
|
|
// if non-proxyable, or is already a proxy, return `value`
|
|
|
|
|
if (typeof value !== 'object' || value === null) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var onchange = options?.onchange;
|
|
|
|
|
|
|
|
|
|
if (STATE_SYMBOL in value) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
value[PROXY_ONCHANGE_SYMBOL](onchange);
|
|
|
|
|
@ -104,10 +102,7 @@ export function proxy(value, options, prev) {
|
|
|
|
|
if (is_proxied_array) {
|
|
|
|
|
// We need to create the length source eagerly to ensure that
|
|
|
|
|
// mutations to the array are properly synced with our proxy
|
|
|
|
|
sources.set(
|
|
|
|
|
'length',
|
|
|
|
|
source(/** @type {any[]} */ (value).length, onchange && { onchange }, stack)
|
|
|
|
|
);
|
|
|
|
|
sources.set('length', source(/** @type {any[]} */ (value).length, onchange, stack));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @type {ProxyMetadata} */
|
|
|
|
|
@ -153,12 +148,12 @@ export function proxy(value, options, prev) {
|
|
|
|
|
var s = sources.get(prop);
|
|
|
|
|
|
|
|
|
|
if (s === undefined) {
|
|
|
|
|
s = with_parent(() => source(descriptor.value, onchange && { onchange }, stack));
|
|
|
|
|
s = with_parent(() => source(descriptor.value, onchange, stack));
|
|
|
|
|
sources.set(prop, s);
|
|
|
|
|
} else {
|
|
|
|
|
set(
|
|
|
|
|
s,
|
|
|
|
|
with_parent(() => proxy(descriptor.value, options))
|
|
|
|
|
with_parent(() => proxy(descriptor.value, onchange))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -172,7 +167,7 @@ export function proxy(value, options, prev) {
|
|
|
|
|
if (prop in target) {
|
|
|
|
|
sources.set(
|
|
|
|
|
prop,
|
|
|
|
|
with_parent(() => source(UNINITIALIZED, onchange && { onchange }, stack))
|
|
|
|
|
with_parent(() => source(UNINITIALIZED, onchange, stack))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
@ -222,9 +217,7 @@ export function proxy(value, options, prev) {
|
|
|
|
|
} else {
|
|
|
|
|
onchange = value;
|
|
|
|
|
for (let [, s] of sources) {
|
|
|
|
|
if (s.o) {
|
|
|
|
|
s.o.onchange = value;
|
|
|
|
|
}
|
|
|
|
|
s.o = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
@ -235,7 +228,7 @@ export function proxy(value, options, 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 = onchange && { onchange };
|
|
|
|
|
let opt = onchange;
|
|
|
|
|
s = with_parent(() =>
|
|
|
|
|
source(proxy(exists ? target[prop] : UNINITIALIZED, opt), opt, stack)
|
|
|
|
|
);
|
|
|
|
|
@ -316,7 +309,7 @@ export function proxy(value, options, prev) {
|
|
|
|
|
(active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
|
|
|
|
|
) {
|
|
|
|
|
if (s === undefined) {
|
|
|
|
|
let opt = onchange && { onchange };
|
|
|
|
|
let opt = onchange;
|
|
|
|
|
s = with_parent(() => source(has ? proxy(target[prop], opt) : UNINITIALIZED, opt, stack));
|
|
|
|
|
sources.set(prop, s);
|
|
|
|
|
}
|
|
|
|
|
@ -355,7 +348,7 @@ export function proxy(value, options, 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 = with_parent(() => source(UNINITIALIZED, onchange && { onchange }, stack));
|
|
|
|
|
other_s = with_parent(() => source(UNINITIALIZED, onchange, stack));
|
|
|
|
|
sources.set(i + '', other_s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -367,7 +360,7 @@ export function proxy(value, options, prev) {
|
|
|
|
|
// object property before writing to that property.
|
|
|
|
|
if (s === undefined) {
|
|
|
|
|
if (!has || get_descriptor(target, prop)?.writable) {
|
|
|
|
|
const opt = onchange && { onchange };
|
|
|
|
|
const opt = onchange;
|
|
|
|
|
s = with_parent(() => source(undefined, opt, stack));
|
|
|
|
|
set(
|
|
|
|
|
s,
|
|
|
|
|
@ -384,7 +377,7 @@ export function proxy(value, options, prev) {
|
|
|
|
|
}
|
|
|
|
|
set(
|
|
|
|
|
s,
|
|
|
|
|
with_parent(() => proxy(value, onchange && { onchange }))
|
|
|
|
|
with_parent(() => proxy(value, onchange))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
@ -450,11 +443,11 @@ export function proxy(value, options, prev) {
|
|
|
|
|
/**
|
|
|
|
|
* @template T
|
|
|
|
|
* @param {T} value
|
|
|
|
|
* @param {ValueOptions} [options]
|
|
|
|
|
* @param {() => void} [onchange]
|
|
|
|
|
* @returns {Source<T>}
|
|
|
|
|
*/
|
|
|
|
|
export function assignable_proxy(value, options) {
|
|
|
|
|
return state(proxy(value, options), options);
|
|
|
|
|
export function assignable_proxy(value, onchange) {
|
|
|
|
|
return state(proxy(value, onchange), onchange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|