Skip to content

Commit e21e624

Browse files
authored
chore: avoid reporting inspections when an exception occurs (#13601)
* chore: avoid reporting inspections when an exception occurs * add test * revise
1 parent bb24544 commit e21e624

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

.changeset/light-crews-deny.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+
chore: avoid reporting inspections when an exception occurs

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { UNINITIALIZED } from '../../../constants.js';
12
import { snapshot } from '../../shared/clone.js';
23
import { inspect_effect, validate_effect } from '../reactivity/effects.js';
34

@@ -12,7 +13,24 @@ export function inspect(get_value, inspector = console.log) {
1213
let initial = true;
1314

1415
inspect_effect(() => {
15-
inspector(initial ? 'init' : 'update', ...snapshot(get_value(), true));
16+
/** @type {any} */
17+
var value = UNINITIALIZED;
18+
19+
// Capturing the value might result in an exception due to the inspect effect being
20+
// sync and thus operating on stale data. In the case we encounter an exception we
21+
// can bail-out of reporting the value. Instead we simply console.error the error
22+
// so at least it's known that an error occured, but we don't stop execution
23+
try {
24+
value = get_value();
25+
} catch (error) {
26+
// eslint-disable-next-line no-console
27+
console.error(error);
28+
}
29+
30+
if (value !== UNINITIALIZED) {
31+
inspector(initial ? 'init' : 'update', ...snapshot(value, true));
32+
}
33+
1634
initial = false;
1735
});
1836
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
let { a = $bindable() } = $props();
3+
4+
$inspect(a).with((t, c) => console.log(t, c));
5+
6+
</script>
7+
<p>{a}</p>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 b1 = target.querySelector('button');
11+
b1?.click();
12+
flushSync();
13+
14+
assert.deepEqual(logs, ['init', 'a', 'init', 'b']);
15+
}
16+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts">
2+
import Component from './Component.svelte';
3+
4+
let s = $state({ a: {"1": "a", "2": "b"} });
5+
</script>
6+
7+
<button onclick={() => (s = {})}>Set State</button>
8+
9+
{#if s.a}
10+
{#each Object.entries(s.a) as [k, v]}
11+
<Component bind:a={s.a[k]} />
12+
{/each}
13+
{/if}

0 commit comments

Comments
 (0)