Skip to content

chore: avoid reporting inspections when an exception occurs #13601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-crews-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: avoid reporting inspections when an exception occurs
20 changes: 19 additions & 1 deletion packages/svelte/src/internal/client/dev/inspect.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UNINITIALIZED } from '../../../constants.js';
import { snapshot } from '../../shared/clone.js';
import { inspect_effect, validate_effect } from '../reactivity/effects.js';

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

inspect_effect(() => {
inspector(initial ? 'init' : 'update', ...snapshot(get_value(), true));
/** @type {any} */
var value = UNINITIALIZED;

// Capturing the value might result in an exception due to the inspect effect being
// sync and thus operating on stale data. In the case we encounter an exception we
// can bail-out of reporting the value. Instead we simply console.error the error
// so at least it's known that an error occured, but we don't stop execution
try {
value = get_value();
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}

if (value !== UNINITIALIZED) {
inspector(initial ? 'init' : 'update', ...snapshot(value, true));
}

initial = false;
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
let { a = $bindable() } = $props();

$inspect(a).with((t, c) => console.log(t, c));

</script>
<p>{a}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},

async test({ assert, target, logs }) {
const b1 = target.querySelector('button');
b1?.click();
flushSync();

assert.deepEqual(logs, ['init', 'a', 'init', 'b']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import Component from './Component.svelte';

let s = $state({ a: {"1": "a", "2": "b"} });
</script>

<button onclick={() => (s = {})}>Set State</button>

{#if s.a}
{#each Object.entries(s.a) as [k, v]}
<Component bind:a={s.a[k]} />
{/each}
{/if}