Skip to content

fix: ensure signal graph is consistent before triggering $inspect signals #13153

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 9 commits into from
Sep 10, 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/chatty-snails-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure signal graph is consistent before triggering $inspect signals
4 changes: 4 additions & 0 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { equals, safe_equals } from './equality.js';
import * as e from '../errors.js';
import { destroy_effect } from './effects.js';
import { inspect_effects, set_inspect_effects } from './sources.js';

/**
* @template V
Expand Down Expand Up @@ -92,6 +93,8 @@ export function update_derived(derived) {
var value;

if (DEV) {
let prev_inspect_effects = inspect_effects;
set_inspect_effects(new Set());
try {
if (stack.includes(derived)) {
e.derived_references_self();
Expand All @@ -102,6 +105,7 @@ export function update_derived(derived) {
destroy_derived_children(derived);
value = update_reaction(derived);
} finally {
set_inspect_effects(prev_inspect_effects);
stack.pop();
}
} else {
Expand Down
26 changes: 21 additions & 5 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
increment_version,
update_effect,
derived_sources,
set_derived_sources
set_derived_sources,
flush_sync
} from '../runtime.js';
import { equals, safe_equals } from './equality.js';
import {
Expand All @@ -29,7 +30,14 @@ import {
} from '../constants.js';
import * as e from '../errors.js';

let inspect_effects = new Set();
export let inspect_effects = new Set();

/**
* @param {Set<any>} v
*/
export function set_inspect_effects(v) {
inspect_effects = v;
}

/**
* @template V
Expand Down Expand Up @@ -159,11 +167,19 @@ export function set(source, value) {
}
}

if (DEV) {
for (const effect of inspect_effects) {
if (DEV && inspect_effects.size > 0) {
const inspects = Array.from(inspect_effects);
if (current_effect === null) {
// Triggering an effect sync can tear the signal graph, so to avoid this we need
// to ensure the graph has been flushed before triggering any inspect effects.
// Only needed when there's currently no effect, and flushing with one present
// could have other unintended consequences, like effects running out of order.
// This is expensive, but given this is a DEV mode only feature, it should be fine
flush_sync();
}
for (const effect of inspects) {
update_effect(effect);
}

inspect_effects.clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test } from '../../test';

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

async test({ assert, logs, target }) {
const [btn] = target.querySelectorAll('button');
btn.click();
btn.click();
await Promise.resolve();

assert.deepEqual(logs, ['init', [], 'update', [{}], 'update', [{}, {}]]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script>
class Rect{
x = $state();
y = $state();

constructor(x, y){
this.x = x;
this.y = y;
}
}

class Node{
pos = $state({ x: 0, y: 0 });
rect = $derived(new Rect(this.pos.x, this.pos.y));

constructor(pos){
this.pos = pos;
}
}

const nodes = $state([]);

const rects = $derived(nodes.map(n => n.rect));

$inspect(rects);
</script>

<button onclick={()=>{
nodes.push(new Node({x: Math.floor(Math.random()*100), y: Math.floor(Math.random()*100)}));
}}>add</button>
<ul>
{#each rects as rect}
<li>{rect.x} - {rect.y}</li>
{/each}
</ul>