-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure signal graph is consistent before triggering $inspect sig…
…nals (#13153) Fixes #13146. Sync effects are not possible with Svelte 5's push-pull system because they can make can break the reactive graph – which is also why we don't permit writes within a derived too. The same problem is occurring here, as inspect effects are run sync – they are actually happening as part of an existing derived – which means they're a bit like writes in a derived and can cause tearing to the reactive signal graph. To avoid this we can call flushSync just before invoking these effects, as that should ensure the graph is made consistent again. Also another issue I found was that we don't "reset" the inspect_effects Set when we enter a derived – which we should as a derived can create it's own local state that should have no bearing on the parent inspect effect.
- Loading branch information
Showing
5 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/svelte/tests/runtime-runes/samples/inspect-recursive/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', [{}, {}]]); | ||
} | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/svelte/tests/runtime-runes/samples/inspect-recursive/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |