Skip to content

Commit 21081d0

Browse files
trueadmRich-Harris
andauthored
fix: ensure $inspect works with SvelteMap and SvelteSet (#12994)
* fix: ensure $inspect works with SvelteMap and SvelteSet * build * dev only * dev only * lint * lint * lint * alternative * Update packages/svelte/src/reactivity/map.js --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 29f2987 commit 21081d0

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

.changeset/green-baboons-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure $inspect works with SvelteMap and SvelteSet

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ function clone(value, cloned, path, paths) {
5656
const unwrapped = cloned.get(value);
5757
if (unwrapped !== undefined) return unwrapped;
5858

59+
if (value instanceof Map) return /** @type {Snapshot<T>} */ (new Map(value));
60+
if (value instanceof Set) return /** @type {Snapshot<T>} */ (new Set(value));
61+
5962
if (is_array(value)) {
6063
const copy = /** @type {Snapshot<any>} */ ([]);
6164
cloned.set(value, copy);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
compileOptions: {
6+
dev: true
7+
},
8+
9+
async test({ assert, target, logs }) {
10+
const [btn, btn2] = target.querySelectorAll('button');
11+
btn.click();
12+
btn2.click();
13+
flushSync();
14+
15+
assert.deepEqual(logs, [
16+
'init',
17+
new Map(),
18+
'init',
19+
new Set(),
20+
'update',
21+
new Map([['a', 'a']]),
22+
'update',
23+
new Set(['a'])
24+
]);
25+
}
26+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
3+
4+
let map = new SvelteMap();
5+
let set = new SvelteSet();
6+
7+
$inspect(map);
8+
$inspect(set);
9+
</script>
10+
11+
<button onclick={() => map.set('a', 'a')}>Map</button>
12+
<button onclick={() => set.add('a')}>Set</button>

0 commit comments

Comments
 (0)