diff --git a/.changeset/empty-coins-build.md b/.changeset/empty-coins-build.md new file mode 100644 index 0000000000..b78d25bb34 --- /dev/null +++ b/.changeset/empty-coins-build.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: improved $inspect handling of reactive Map/Set/Date diff --git a/packages/svelte/src/internal/client/dev/inspect.js b/packages/svelte/src/internal/client/dev/inspect.js index c9b9411786..3e01dc5b44 100644 --- a/packages/svelte/src/internal/client/dev/inspect.js +++ b/packages/svelte/src/internal/client/dev/inspect.js @@ -1,6 +1,7 @@ +import { DEV } from 'esm-env'; import { snapshot } from '../proxy.js'; import { render_effect, validate_effect } from '../reactivity/effects.js'; -import { current_effect, deep_read, untrack } from '../runtime.js'; +import { deep_read, untrack } from '../runtime.js'; import { array_prototype, get_prototype_of, object_prototype } from '../utils.js'; /** @type {Function | null} */ @@ -61,6 +62,16 @@ export function inspect(get_value, inspector = console.log) { */ function deep_snapshot(value, visited = new Map()) { if (typeof value === 'object' && value !== null && !visited.has(value)) { + if (DEV) { + // When dealing with ReactiveMap or ReactiveSet, return normal versions + // so that console.log provides better output versions + if (value instanceof Map && value.constructor !== Map) { + return new Map(value); + } + if (value instanceof Set && value.constructor !== Set) { + return new Set(value); + } + } const unstated = snapshot(value); if (unstated !== value) { diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 2b5496a990..9fd28a3cda 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -1215,6 +1215,11 @@ export function deep_read(value, visited = new Set()) { !visited.has(value) ) { visited.add(value); + // When working with a possible ReactiveDate, this + // will ensure we capture changes to it. + if (value instanceof Date) { + value.getTime(); + } for (let key in value) { try { deep_read(value[key], visited);