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 <rich.harris@vercel.com>
pull/13008/head
Dominic Gannaway 5 months ago committed by GitHub
parent 29f29878c3
commit 21081d0fa5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure $inspect works with SvelteMap and SvelteSet

@ -56,6 +56,9 @@ function clone(value, cloned, path, paths) {
const unwrapped = cloned.get(value);
if (unwrapped !== undefined) return unwrapped;
if (value instanceof Map) return /** @type {Snapshot<T>} */ (new Map(value));
if (value instanceof Set) return /** @type {Snapshot<T>} */ (new Set(value));
if (is_array(value)) {
const copy = /** @type {Snapshot<any>} */ ([]);
cloned.set(value, copy);

@ -0,0 +1,26 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
compileOptions: {
dev: true
},
async test({ assert, target, logs }) {
const [btn, btn2] = target.querySelectorAll('button');
btn.click();
btn2.click();
flushSync();
assert.deepEqual(logs, [
'init',
new Map(),
'init',
new Set(),
'update',
new Map([['a', 'a']]),
'update',
new Set(['a'])
]);
}
});

@ -0,0 +1,12 @@
<script>
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
let map = new SvelteMap();
let set = new SvelteSet();
$inspect(map);
$inspect(set);
</script>
<button onclick={() => map.set('a', 'a')}>Map</button>
<button onclick={() => set.add('a')}>Set</button>
Loading…
Cancel
Save