Skip to content

Commit fbf8a13

Browse files
committed
apply suggestion
1 parent 8e5f1db commit fbf8a13

File tree

2 files changed

+22
-54
lines changed

2 files changed

+22
-54
lines changed

packages/svelte/src/internal/client/dev/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function inspect(get_value, inspector = console.log) {
2626
return;
2727
}
2828

29-
var snap = snapshot(value, true, true);
29+
var snap = snapshot(value, true);
3030
untrack(() => {
3131
inspector(initial ? 'init' : 'update', ...snap);
3232
});

packages/svelte/src/internal/shared/clone.js

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ const empty = [];
1515
* @template T
1616
* @param {T} value
1717
* @param {boolean} [skip_warning]
18-
* @param {boolean} [only_deproxy]
18+
* @param {boolean} [no_tojson]
1919
* @returns {Snapshot<T>}
2020
*/
21-
export function snapshot(value, skip_warning = false, only_deproxy = false) {
21+
export function snapshot(value, skip_warning = false, no_tojson = false) {
2222
if (DEV && !skip_warning) {
2323
/** @type {string[]} */
2424
const paths = [];
2525

26-
const copy = clone(value, new Map(), '', paths);
26+
const copy = clone(value, new Map(), '', paths, null, no_tojson);
2727
if (paths.length === 1 && paths[0] === '') {
2828
// value could not be cloned
2929
w.state_snapshot_uncloneable();
@@ -41,7 +41,7 @@ export function snapshot(value, skip_warning = false, only_deproxy = false) {
4141
return copy;
4242
}
4343

44-
return clone(value, new Map(), '', empty, null, only_deproxy);
44+
return clone(value, new Map(), '', empty, null, no_tojson);
4545
}
4646

4747
/**
@@ -51,18 +51,16 @@ export function snapshot(value, skip_warning = false, only_deproxy = false) {
5151
* @param {string} path
5252
* @param {string[]} paths
5353
* @param {null | T} [original] The original value, if `value` was produced from a `toJSON` call
54-
* @param {boolean} [only_deproxy] Don't clone objects that aren't proxies
54+
* @param {boolean} [no_tojson]
5555
* @returns {Snapshot<T>}
5656
*/
57-
function clone(value, cloned, path, paths, original = null, only_deproxy = false) {
57+
function clone(value, cloned, path, paths, original = null, no_tojson = false) {
5858
if (typeof value === 'object' && value !== null) {
5959
var unwrapped = cloned.get(value);
6060
if (unwrapped !== undefined) return unwrapped;
6161

62-
if (value instanceof Map)
63-
return /** @type {Snapshot<T>} */ (only_deproxy ? value : new Map(value));
64-
if (value instanceof Set)
65-
return /** @type {Snapshot<T>} */ (only_deproxy ? value : new Set(value));
62+
if (value instanceof Map) return /** @type {Snapshot<T>} */ (new Map(value));
63+
if (value instanceof Set) return /** @type {Snapshot<T>} */ (new Set(value));
6664

6765
if (is_array(value)) {
6866
var copy = /** @type {Snapshot<any>} */ (Array(value.length));
@@ -75,7 +73,7 @@ function clone(value, cloned, path, paths, original = null, only_deproxy = false
7573
for (var i = 0; i < value.length; i += 1) {
7674
var element = value[i];
7775
if (i in value) {
78-
copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths, null, only_deproxy);
76+
copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths, null, no_tojson);
7977
}
8078
}
8179

@@ -92,49 +90,26 @@ function clone(value, cloned, path, paths, original = null, only_deproxy = false
9290
}
9391

9492
for (var key in value) {
95-
copy[key] = clone(
96-
// @ts-expect-error
97-
value[key],
98-
cloned,
99-
DEV ? `${path}.${key}` : path,
100-
paths,
101-
null,
102-
only_deproxy
103-
);
93+
// @ts-expect-error
94+
copy[key] = clone(value[key], cloned, DEV ? `${path}.${key}` : path, paths, null, no_tojson);
10495
}
10596

10697
return copy;
10798
}
10899

109100
if (value instanceof Date) {
110-
if (only_deproxy) {
111-
structuredClone(value);
112-
} else {
113-
return /** @type {Snapshot<T>} */ (structuredClone(value));
114-
}
101+
return /** @type {Snapshot<T>} */ (structuredClone(value));
115102
}
116103

117-
if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function') {
118-
if (!only_deproxy) {
119-
return clone(
120-
/** @type {T & { toJSON(): any } } */ (value).toJSON(),
121-
cloned,
122-
DEV ? `${path}.toJSON()` : path,
123-
paths,
124-
// Associate the instance with the toJSON clone
125-
value
126-
);
127-
} else {
128-
// we still want to read each property
129-
clone(
130-
/** @type {T & { toJSON(): any } } */ (value).toJSON(),
131-
cloned,
132-
DEV ? `${path}.toJSON()` : path,
133-
paths,
134-
// Associate the instance with the toJSON clone
135-
value
136-
);
137-
}
104+
if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function' && !no_tojson) {
105+
return clone(
106+
/** @type {T & { toJSON(): any } } */ (value).toJSON(),
107+
cloned,
108+
DEV ? `${path}.toJSON()` : path,
109+
paths,
110+
// Associate the instance with the toJSON clone
111+
value
112+
);
138113
}
139114
}
140115

@@ -143,13 +118,6 @@ function clone(value, cloned, path, paths, original = null, only_deproxy = false
143118
return /** @type {Snapshot<T>} */ (value);
144119
}
145120

146-
if (only_deproxy) {
147-
try {
148-
structuredClone(value);
149-
} catch {}
150-
return /** @type {Snapshot<T>} */ (value);
151-
}
152-
153121
try {
154122
return /** @type {Snapshot<T>} */ (structuredClone(value));
155123
} catch (e) {

0 commit comments

Comments
 (0)