Skip to content

Commit ad578a5

Browse files
adigubatrueadm
andauthored
fix: @debug does not work with proxied-state (#13690)
* fix: @debug must use $state.snapshot() on value * changeset * add test --------- Co-authored-by: Dominic Gannaway <[email protected]>
1 parent 28c8d2b commit ad578a5

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

.changeset/sharp-tigers-notice.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+
fix: @debug does not work with proxied-state

packages/svelte/src/compiler/phases/3-transform/client/visitors/DebugTag.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import * as b from '../../../../utils/builders.js';
99
*/
1010
export function DebugTag(node, context) {
1111
const object = b.object(
12-
node.identifiers.map((identifier) =>
13-
b.prop('init', identifier, /** @type {Expression} */ (context.visit(identifier)))
14-
)
12+
node.identifiers.map((identifier) => {
13+
const visited = b.call('$.snapshot', /** @type {Expression} */ (context.visit(identifier)));
14+
15+
return b.prop(
16+
'init',
17+
identifier,
18+
context.state.analysis.runes ? visited : b.call('$.untrack', b.thunk(visited))
19+
);
20+
})
1521
);
1622

1723
const call = b.call('console.log', object);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
compileOptions: {
6+
dev: true
7+
},
8+
9+
test({ assert, target, logs }) {
10+
const b1 = target.querySelector('button');
11+
b1?.click();
12+
flushSync();
13+
b1?.click();
14+
flushSync();
15+
16+
assert.deepEqual(logs, [
17+
{ count: { current: 0 } },
18+
{ count: { current: 1 } },
19+
{ count: { current: 2 } }
20+
]);
21+
}
22+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let count = $state({ current: 0 });
3+
</script>
4+
5+
{@debug count}
6+
7+
<button onclick={()=> count.current++}>+</button>

0 commit comments

Comments
 (0)