Skip to content

Commit 5033c8e

Browse files
authored
fix: tag stores for $inspect.trace() (#16452)
1 parent dd55de3 commit 5033c8e

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

.changeset/silent-rockets-tease.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: tag stores for `$inspect.trace()`

packages/svelte/src/internal/client/dev/tracing.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function log_entry(signal, entry) {
2626
return;
2727
}
2828

29-
const type = (signal.f & (DERIVED | ASYNC)) !== 0 ? '$derived' : '$state';
29+
const type = get_type(signal);
3030
const current_reaction = /** @type {Reaction} */ (active_reaction);
3131
const dirty = signal.wv > current_reaction.wv || current_reaction.wv === 0;
3232
const style = dirty
@@ -73,6 +73,15 @@ function log_entry(signal, entry) {
7373
console.groupEnd();
7474
}
7575

76+
/**
77+
* @param {Value} signal
78+
* @returns {'$state' | '$derived' | 'store'}
79+
*/
80+
function get_type(signal) {
81+
if ((signal.f & (DERIVED | ASYNC)) !== 0) return '$derived';
82+
return signal.label?.startsWith('$') ? 'store' : '$state';
83+
}
84+
7685
/**
7786
* @template T
7887
* @param {() => string} label

packages/svelte/src/internal/client/reactivity/store.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { define_property, noop } from '../../shared/utils.js';
66
import { get } from '../runtime.js';
77
import { teardown } from './effects.js';
88
import { mutable_source, set } from './sources.js';
9+
import { DEV } from 'esm-env';
910

1011
/**
1112
* Whether or not the prop currently being read is a store binding, as in
@@ -33,6 +34,10 @@ export function store_get(store, store_name, stores) {
3334
unsubscribe: noop
3435
});
3536

37+
if (DEV) {
38+
entry.source.label = store_name;
39+
}
40+
3641
// if the component that setup this is already unmounted we don't want to register a subscription
3742
if (entry.store !== store && !(IS_UNMOUNTED in stores)) {
3843
entry.unsubscribe();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
import { normalise_trace_logs } from '../../../helpers.js';
4+
5+
export default test({
6+
compileOptions: {
7+
dev: true
8+
},
9+
10+
test({ assert, target, logs }) {
11+
assert.deepEqual(normalise_trace_logs(logs), [
12+
{ log: 'effect' },
13+
{ log: 'store', highlighted: true },
14+
{ log: '$count', highlighted: false },
15+
{ log: 0 }
16+
]);
17+
18+
logs.length = 0;
19+
20+
const [button] = target.querySelectorAll('button');
21+
flushSync(() => button.click());
22+
23+
assert.deepEqual(normalise_trace_logs(logs), [
24+
{ log: 'effect' },
25+
{ log: 'store', highlighted: true },
26+
{ log: '$count', highlighted: false },
27+
{ log: 1 }
28+
]);
29+
}
30+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
4+
const count = writable(0);
5+
6+
$effect(() => {
7+
$inspect.trace('effect');
8+
$count;
9+
});
10+
</script>
11+
12+
<button onclick={() => $count += 1}>
13+
clicks: {$count}
14+
</button>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy