Skip to content

Commit

Permalink
fix: improved $inspect handling of reactive Map/Set/Date (#11553)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm committed May 11, 2024
1 parent 7e9b109 commit 597715f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/empty-coins-build.md
@@ -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
@@ -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
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

0 comments on commit 597715f

Please sign in to comment.