Skip to content

fix: improved $inspect handling of reactive Map/Set/Date #11553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-coins-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improved $inspect handling of reactive Map/Set/Date
13 changes: 12 additions & 1 deletion packages/svelte/src/internal/client/dev/inspect.js
Original file line number Diff line number Diff line change
@@ -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} */
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down