try suggestion

pull/16622/head
ComputerGuy 3 weeks ago
parent c745945e9e
commit e862e5e044

@ -179,7 +179,7 @@ export function get_stack(label) {
*/ */
export function tag(source, label) { export function tag(source, label) {
source.label = label; source.label = label;
tag_proxy(source.v, label, source); tag_proxy(source.v, label);
return source; return source;
} }
@ -187,11 +187,10 @@ export function tag(source, label) {
/** /**
* @param {unknown} value * @param {unknown} value
* @param {string} label * @param {string} label
* @param {Value} source
*/ */
export function tag_proxy(value, label, source) { export function tag_proxy(value, label) {
// @ts-expect-error // @ts-expect-error
value?.[PROXY_PATH_SYMBOL]?.(label, source); value?.[PROXY_PATH_SYMBOL]?.(label);
return value; return value;
} }

@ -34,13 +34,9 @@ const regex_is_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
/** /**
* @template T * @template T
* @param {T} value * @param {T} value
* @param {WeakSet<Source>} [owned_sources]
* @returns {T} * @returns {T}
*/ */
export function proxy(value, owned_sources) { export function proxy(value) {
if (DEV) {
owned_sources ??= new WeakSet();
}
// if non-proxyable, or is already a proxy, return `value` // if non-proxyable, or is already a proxy, return `value`
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) { if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
return value; return value;
@ -97,16 +93,11 @@ export function proxy(value, owned_sources) {
/** Used in dev for $inspect.trace() */ /** Used in dev for $inspect.trace() */
var path = ''; var path = '';
let updating = false;
/** /** @param {string} new_path */
* @param {string} new_path function update_path(new_path) {
* @param {Source} updater the source causing the path update if (updating) return;
*/ updating = true;
function update_path(new_path, updater) {
// there's a circular reference somewhere, don't update the path to avoid recursion
if (owned_sources?.has(updater)) {
return;
}
path = new_path; path = new_path;
tag(version, `${path} version`); tag(version, `${path} version`);
@ -115,6 +106,7 @@ export function proxy(value, owned_sources) {
for (const [prop, source] of sources) { for (const [prop, source] of sources) {
tag(source, get_label(path, prop)); tag(source, get_label(path, prop));
} }
updating = false;
} }
return new Proxy(/** @type {any} */ (value), { return new Proxy(/** @type {any} */ (value), {
@ -138,7 +130,6 @@ export function proxy(value, owned_sources) {
sources.set(prop, s); sources.set(prop, s);
if (DEV && typeof prop === 'string') { if (DEV && typeof prop === 'string') {
tag(s, get_label(path, prop)); tag(s, get_label(path, prop));
owned_sources?.add(s);
} }
return s; return s;
}); });
@ -160,7 +151,6 @@ export function proxy(value, owned_sources) {
if (DEV) { if (DEV) {
tag(s, get_label(path, prop)); tag(s, get_label(path, prop));
owned_sources?.add(s);
} }
} }
} else { } else {
@ -186,12 +176,11 @@ export function proxy(value, owned_sources) {
// 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)) {
s = with_parent(() => { s = with_parent(() => {
var p = proxy(exists ? target[prop] : UNINITIALIZED, DEV ? owned_sources : undefined); var p = proxy(exists ? target[prop] : UNINITIALIZED);
var s = source(p, stack); var s = source(p, stack);
if (DEV) { if (DEV) {
tag(s, get_label(path, prop)); tag(s, get_label(path, prop));
owned_sources?.add(s);
} }
return s; return s;
@ -245,12 +234,11 @@ export function proxy(value, owned_sources) {
) { ) {
if (s === undefined) { if (s === undefined) {
s = with_parent(() => { s = with_parent(() => {
var p = has ? proxy(target[prop], DEV ? owned_sources : undefined) : UNINITIALIZED; var p = has ? proxy(target[prop]) : UNINITIALIZED;
var s = source(p, stack); var s = source(p, stack);
if (DEV) { if (DEV) {
tag(s, get_label(path, prop)); tag(s, get_label(path, prop));
owned_sources?.add(s);
} }
return s; return s;
@ -287,7 +275,6 @@ export function proxy(value, owned_sources) {
if (DEV) { if (DEV) {
tag(other_s, get_label(path, i)); tag(other_s, get_label(path, i));
owned_sources?.add(other_s);
} }
} }
} }
@ -300,19 +287,18 @@ export function proxy(value, owned_sources) {
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, stack)); s = with_parent(() => source(undefined, stack));
set(s, proxy(value, DEV ? owned_sources : undefined)); set(s, proxy(value));
sources.set(prop, s); sources.set(prop, s);
if (DEV) { if (DEV) {
tag(s, get_label(path, prop)); tag(s, get_label(path, prop));
owned_sources?.add(s);
} }
} }
} else { } else {
has = s.v !== UNINITIALIZED; has = s.v !== UNINITIALIZED;
var p = with_parent(() => proxy(value, DEV ? owned_sources : undefined)); var p = with_parent(() => proxy(value));
set(s, p); set(s, p);
} }

@ -156,7 +156,7 @@ export function set(source, value, should_proxy = false) {
let new_value = should_proxy ? proxy(value) : value; let new_value = should_proxy ? proxy(value) : value;
if (DEV) { if (DEV) {
tag_proxy(new_value, /** @type {string} */ (source.label), source); tag_proxy(new_value, /** @type {string} */ (source.label));
} }
return internal_set(source, new_value); return internal_set(source, new_value);

Loading…
Cancel
Save