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

pull/11557/head
Dominic Gannaway 2 months ago committed by GitHub
parent 7e9b109de6
commit 597715ff98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: improved $inspect handling of reactive Map/Set/Date

@ -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) {

@ -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);

Loading…
Cancel
Save