From ff2e489883d71b6954602ca6c1cd8948b0ca3698 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 18 Apr 2024 16:51:36 -0400 Subject: [PATCH] deep snapshots --- .../svelte/src/internal/client/dev/inspect.js | 2 +- .../src/internal/client/reactivity/snapshot.js | 16 +++++++++++----- packages/svelte/src/internal/client/runtime.js | 5 ++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/svelte/src/internal/client/dev/inspect.js b/packages/svelte/src/internal/client/dev/inspect.js index bc3c4edca8..73ae94e239 100644 --- a/packages/svelte/src/internal/client/dev/inspect.js +++ b/packages/svelte/src/internal/client/dev/inspect.js @@ -31,7 +31,7 @@ export function inspect(get_value, inspector = console.log) { // calling `inspector` directly inside the effect, so that // we get useful stack traces var fn = () => { - const value = snapshot(get_value()); + const value = snapshot(get_value(), true); inspector(initial ? 'init' : 'update', ...value); }; diff --git a/packages/svelte/src/internal/client/reactivity/snapshot.js b/packages/svelte/src/internal/client/reactivity/snapshot.js index 2fb55b2708..009838e7bd 100644 --- a/packages/svelte/src/internal/client/reactivity/snapshot.js +++ b/packages/svelte/src/internal/client/reactivity/snapshot.js @@ -1,18 +1,24 @@ import { STATE_SYMBOL } from '../constants.js'; -import { is_array } from '../utils.js'; +import { array_prototype, get_prototype_of, is_array, object_prototype } from '../utils.js'; /** * @template {any} T * @param {T} value + * @param {boolean} deep * @param {Map} values * @returns {T} */ -export function snapshot(value, values = new Map()) { +export function snapshot(value, deep = false, values = new Map()) { if (typeof value !== 'object' || value === null) { return value; } - if (STATE_SYMBOL in value) { + var proto = get_prototype_of(value); + + if ( + (proto === object_prototype || proto === array_prototype) && + (deep || STATE_SYMBOL in value) + ) { var unwrapped = /** @type {T} */ (values.get(value)); if (unwrapped !== undefined) { return unwrapped; @@ -25,7 +31,7 @@ export function snapshot(value, values = new Map()) { values.set(value, array); for (var i = 0; i < length; i += 1) { - array[i] = snapshot(value[i], values); + array[i] = snapshot(value[i], deep, values); } return /** @type {T} */ (array); @@ -36,7 +42,7 @@ export function snapshot(value, values = new Map()) { values.set(value, obj); for (var [k, v] of Object.entries(value)) { - obj[k] = snapshot(v, values); + obj[k] = snapshot(v, deep, values); } return /** @type {T} */ (obj); diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 6fd0e4c476..1a3ae8e581 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -125,9 +125,8 @@ export function is_runes() { */ export function batch_inspect(target, prop, receiver) { const value = Reflect.get(target, prop, receiver); - /** - * @this {any} - */ + + /** @this {any} */ return function () { const previously_batching_effect = is_batching_effect; is_batching_effect = true;