Skip to content
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/twelve-cooks-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ignore false-positive errors of `$inspect` dependencies
2 changes: 2 additions & 0 deletions packages/svelte/src/internal/client/dev/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function inspect(get_value, inspector, show_stack = false) {
// in an error (an `$inspect(object.property)` will run before the
// `{#if object}...{/if}` that contains it)
eager_effect(() => {
error = UNINITIALIZED;

try {
var value = get_value();
} catch (e) {
Expand Down
19 changes: 18 additions & 1 deletion packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,24 @@ export function flush_eager_effects() {
eager_effects_deferred = false;

for (const effect of eager_effects) {
if (is_dirty(effect)) {
// Mark clean inspect-effects as maybe dirty and then check their dirtiness
// instead of just updating the effects - this way we avoid overfiring.
if ((effect.f & CLEAN) !== 0) {
set_signal_status(effect, MAYBE_DIRTY);
}

let dirty;

try {
dirty = is_dirty(effect);
} catch {
// Dirty-checking can evaluate derived dependencies and throw in cases where
// parent effects are about to destroy this eager effect. Run the effect so
// its own error handling can deal with transient failures.
dirty = true;
}

if (dirty) {
update_effect(effect);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const async_mode = process.env.SVELTE_NO_ASYNC !== 'true';
* @param {any[]} logs
*/
export function normalise_inspect_logs(logs) {
/** @type {string[]} */
/** @type {any[]} */
const normalised = [];

for (const log of logs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
let {things} = $props();

$inspect(things);
</script>

<ul>
{#each things as thing}
<li>thing {thing.id}</li>
{/each}
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { normalise_inspect_logs } from '../../../helpers';
import { test } from '../../test';
import { flushSync } from 'svelte';

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

async test({ assert, target, errors, logs }) {
const button = target.querySelector('button');

flushSync(() => {
button?.click();
});

assert.htmlEqual(target.innerHTML, '<button>clear</button>');
assert.equal(errors.length, 0);
assert.deepEqual(normalise_inspect_logs(logs), [[{ id: 1 }, { id: 2 }]]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import List from "./List.svelte"

let data = $state({things: [{id:1}, {id:2}]})

function reloadData() {
data = null
}
</script>

{#if data}
<List things={data.things.map((t) => t)} />
{/if}

<button onclick={() => reloadData()}>clear</button>
Loading