diff --git a/packages/svelte/src/internal/client/dev/inspect.js b/packages/svelte/src/internal/client/dev/inspect.js index f79cf47299..c593f2622c 100644 --- a/packages/svelte/src/internal/client/dev/inspect.js +++ b/packages/svelte/src/internal/client/dev/inspect.js @@ -26,7 +26,7 @@ export function inspect(get_value, inspector = console.log) { return; } - var snap = snapshot(value, true, true); + var snap = snapshot(value, true); untrack(() => { inspector(initial ? 'init' : 'update', ...snap); }); diff --git a/packages/svelte/src/internal/shared/clone.js b/packages/svelte/src/internal/shared/clone.js index 7dc9ffec2a..b5964f8ec2 100644 --- a/packages/svelte/src/internal/shared/clone.js +++ b/packages/svelte/src/internal/shared/clone.js @@ -15,15 +15,15 @@ const empty = []; * @template T * @param {T} value * @param {boolean} [skip_warning] - * @param {boolean} [only_deproxy] + * @param {boolean} [no_tojson] * @returns {Snapshot} */ -export function snapshot(value, skip_warning = false, only_deproxy = false) { +export function snapshot(value, skip_warning = false, no_tojson = false) { if (DEV && !skip_warning) { /** @type {string[]} */ const paths = []; - const copy = clone(value, new Map(), '', paths); + const copy = clone(value, new Map(), '', paths, null, no_tojson); if (paths.length === 1 && paths[0] === '') { // value could not be cloned w.state_snapshot_uncloneable(); @@ -41,7 +41,7 @@ export function snapshot(value, skip_warning = false, only_deproxy = false) { return copy; } - return clone(value, new Map(), '', empty, null, only_deproxy); + return clone(value, new Map(), '', empty, null, no_tojson); } /** @@ -51,18 +51,16 @@ export function snapshot(value, skip_warning = false, only_deproxy = false) { * @param {string} path * @param {string[]} paths * @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 + * @param {boolean} [no_tojson] * @returns {Snapshot} */ -function clone(value, cloned, path, paths, original = null, only_deproxy = false) { +function clone(value, cloned, path, paths, original = null, no_tojson = false) { if (typeof value === 'object' && value !== null) { var unwrapped = cloned.get(value); if (unwrapped !== undefined) return unwrapped; - if (value instanceof Map) - return /** @type {Snapshot} */ (only_deproxy ? value : new Map(value)); - if (value instanceof Set) - return /** @type {Snapshot} */ (only_deproxy ? value : new Set(value)); + if (value instanceof Map) return /** @type {Snapshot} */ (new Map(value)); + if (value instanceof Set) return /** @type {Snapshot} */ (new Set(value)); if (is_array(value)) { var copy = /** @type {Snapshot} */ (Array(value.length)); @@ -75,7 +73,7 @@ function clone(value, cloned, path, paths, original = null, only_deproxy = false 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, null, only_deproxy); + copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths, null, no_tojson); } } @@ -92,49 +90,26 @@ function clone(value, cloned, path, paths, original = null, only_deproxy = false } for (var key in value) { - copy[key] = clone( - // @ts-expect-error - value[key], - cloned, - DEV ? `${path}.${key}` : path, - paths, - null, - only_deproxy - ); + // @ts-expect-error + copy[key] = clone(value[key], cloned, DEV ? `${path}.${key}` : path, paths, null, no_tojson); } return copy; } if (value instanceof Date) { - if (only_deproxy) { - structuredClone(value); - } else { - return /** @type {Snapshot} */ (structuredClone(value)); - } + return /** @type {Snapshot} */ (structuredClone(value)); } - if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function') { - 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 - ); - } + if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function' && !no_tojson) { + return clone( + /** @type {T & { toJSON(): any } } */ (value).toJSON(), + cloned, + DEV ? `${path}.toJSON()` : path, + paths, + // Associate the instance with the toJSON clone + value + ); } } @@ -143,13 +118,6 @@ function clone(value, cloned, path, paths, original = null, only_deproxy = false return /** @type {Snapshot} */ (value); } - if (only_deproxy) { - try { - structuredClone(value); - } catch {} - return /** @type {Snapshot} */ (value); - } - try { return /** @type {Snapshot} */ (structuredClone(value)); } catch (e) {