fix: don't clone non-proxies in `$inspect`

pull/16617/head
ComputerGuy 3 weeks ago
parent 2e02868ef1
commit 8e5f1db50c

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't clone non-proxies in `$inspect`

@ -26,7 +26,7 @@ export function inspect(get_value, inspector = console.log) {
return;
}
var snap = snapshot(value, true);
var snap = snapshot(value, true, true);
untrack(() => {
inspector(initial ? 'init' : 'update', ...snap);
});

@ -15,9 +15,10 @@ const empty = [];
* @template T
* @param {T} value
* @param {boolean} [skip_warning]
* @param {boolean} [only_deproxy]
* @returns {Snapshot<T>}
*/
export function snapshot(value, skip_warning = false) {
export function snapshot(value, skip_warning = false, only_deproxy = false) {
if (DEV && !skip_warning) {
/** @type {string[]} */
const paths = [];
@ -40,7 +41,7 @@ export function snapshot(value, skip_warning = false) {
return copy;
}
return clone(value, new Map(), '', empty);
return clone(value, new Map(), '', empty, null, only_deproxy);
}
/**
@ -49,16 +50,19 @@ export function snapshot(value, skip_warning = false) {
* @param {Map<T, Snapshot<T>>} cloned
* @param {string} path
* @param {string[]} paths
* @param {null | T} original The original value, if `value` was produced from a `toJSON` call
* @param {null | T} [original] The original value, if `value` was produced from a `toJSON` call
* @param {boolean} [only_deproxy] Don't clone objects that aren't proxies
* @returns {Snapshot<T>}
*/
function clone(value, cloned, path, paths, original = null) {
function clone(value, cloned, path, paths, original = null, only_deproxy = false) {
if (typeof value === 'object' && value !== null) {
var unwrapped = cloned.get(value);
if (unwrapped !== undefined) return unwrapped;
if (value instanceof Map) return /** @type {Snapshot<T>} */ (new Map(value));
if (value instanceof Set) return /** @type {Snapshot<T>} */ (new Set(value));
if (value instanceof Map)
return /** @type {Snapshot<T>} */ (only_deproxy ? value : new Map(value));
if (value instanceof Set)
return /** @type {Snapshot<T>} */ (only_deproxy ? value : new Set(value));
if (is_array(value)) {
var copy = /** @type {Snapshot<any>} */ (Array(value.length));
@ -71,7 +75,7 @@ function clone(value, cloned, path, paths, original = null) {
for (var i = 0; i < value.length; i += 1) {
var element = value[i];
if (i in value) {
copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths);
copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths, null, only_deproxy);
}
}
@ -88,26 +92,49 @@ function clone(value, cloned, path, paths, original = null) {
}
for (var key in value) {
// @ts-expect-error
copy[key] = clone(value[key], cloned, DEV ? `${path}.${key}` : path, paths);
copy[key] = clone(
// @ts-expect-error
value[key],
cloned,
DEV ? `${path}.${key}` : path,
paths,
null,
only_deproxy
);
}
return copy;
}
if (value instanceof Date) {
return /** @type {Snapshot<T>} */ (structuredClone(value));
if (only_deproxy) {
structuredClone(value);
} else {
return /** @type {Snapshot<T>} */ (structuredClone(value));
}
}
if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function') {
return clone(
/** @type {T & { toJSON(): any } } */ (value).toJSON(),
cloned,
DEV ? `${path}.toJSON()` : path,
paths,
// Associate the instance with the toJSON clone
value
);
if (!only_deproxy) {
return clone(
/** @type {T & { toJSON(): any } } */ (value).toJSON(),
cloned,
DEV ? `${path}.toJSON()` : path,
paths,
// Associate the instance with the toJSON clone
value
);
} else {
// we still want to read each property
clone(
/** @type {T & { toJSON(): any } } */ (value).toJSON(),
cloned,
DEV ? `${path}.toJSON()` : path,
paths,
// Associate the instance with the toJSON clone
value
);
}
}
}
@ -116,6 +143,13 @@ function clone(value, cloned, path, paths, original = null) {
return /** @type {Snapshot<T>} */ (value);
}
if (only_deproxy) {
try {
structuredClone(value);
} catch {}
return /** @type {Snapshot<T>} */ (value);
}
try {
return /** @type {Snapshot<T>} */ (structuredClone(value));
} catch (e) {

Loading…
Cancel
Save