Skip to content

Commit 1701077

Browse files
committed
WIP
1 parent adaa69a commit 1701077

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { snapshot } from '../../shared/clone.js';
22
import { STATE_SYMBOL } from '../constants.js';
33

4-
/** @type { { label: string, time: number, sub: any, stack: string | void, value: any }[] | null } */
4+
export const NOT_REACTIVE = 0;
5+
export const REACTIVE_UNCHANGED = 1;
6+
export const REACTIVE_CHANGED = 2;
7+
8+
/** @type { { changed: boolean, label: string, time: number, sub: any, stack: string | void, value: any }[] | null } */
59
export let tracing_expressions = null;
6-
export let tracing_expression_reactive = false;
10+
/** @type { 0 | 1 | 2 } */
11+
export let tracing_expression_reactive = NOT_REACTIVE;
712

813
/**
914
* @param {any} expressions
@@ -13,18 +18,21 @@ function log_expressions(expressions) {
1318
const val = expression.value;
1419
const label = expression.label;
1520
const time = expression.time;
21+
const changed = expression.changed;
1622

1723
if (time) {
1824
// eslint-disable-next-line no-console
1925
console.groupCollapsed(
20-
`${label} %c${time.toFixed(2)}ms`,
26+
`%c${label} %c${time.toFixed(2)}ms`,
27+
!changed ? 'color: darkgrey; font-weight: normal' : undefined,
2128
'color: grey',
2229
val && typeof val === 'object' && STATE_SYMBOL in val ? snapshot(val, true) : val
2330
);
2431
} else {
2532
// eslint-disable-next-line no-console
2633
console.groupCollapsed(
27-
label,
34+
`%c${label}`,
35+
!changed ? 'color: darkgrey; font-weight: normal' : undefined,
2836
val && typeof val === 'object' && STATE_SYMBOL in val ? snapshot(val, true) : val
2937
);
3038
}
@@ -79,7 +87,7 @@ export function trace(fn, label, computed) {
7987
var previously_tracing_expression_reactive = tracing_expression_reactive;
8088

8189
try {
82-
tracing_expression_reactive = false;
90+
tracing_expression_reactive = NOT_REACTIVE;
8391
tracing_expressions = [];
8492
var value,
8593
time = 0;
@@ -98,8 +106,15 @@ export function trace(fn, label, computed) {
98106
?.split('\n')
99107
[/Firefox/.test(navigator.userAgent) ? 3 : 2].trimStart();
100108

101-
if (tracing_expression_reactive) {
102-
tracing_expressions.push({ label, value, time, stack, sub: null });
109+
if (tracing_expression_reactive !== NOT_REACTIVE) {
110+
tracing_expressions.push({
111+
changed: tracing_expression_reactive === REACTIVE_CHANGED,
112+
label,
113+
value,
114+
time,
115+
stack,
116+
sub: null
117+
});
103118

104119
if (previously_tracing_expressions !== null) {
105120
previously_tracing_expressions.push(...tracing_expressions);
@@ -109,6 +124,7 @@ export function trace(fn, label, computed) {
109124
previously_tracing_expressions.length !== 0
110125
) {
111126
previously_tracing_expressions.push({
127+
changed: tracing_expressions.some(e => e.changed),
112128
label,
113129
value,
114130
time,
@@ -125,6 +141,9 @@ export function trace(fn, label, computed) {
125141
}
126142
}
127143

128-
export function tracing_reactive_signal() {
129-
tracing_expression_reactive = true;
144+
/**
145+
* @param {0 | 1 | 2} value
146+
*/
147+
export function set_tracing_expression_reactive(value) {
148+
tracing_expression_reactive = value;
130149
}

packages/svelte/src/internal/client/runtime.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ import * as e from './errors.js';
3434
import { lifecycle_outside_component } from '../shared/errors.js';
3535
import { FILENAME } from '../../constants.js';
3636
import { legacy_mode_flag } from '../flags/index.js';
37-
import { tracing_expressions, tracing_reactive_signal } from './dev/tracing.js';
37+
import {
38+
tracing_expressions,
39+
set_tracing_expression_reactive,
40+
REACTIVE_UNCHANGED,
41+
REACTIVE_CHANGED
42+
} from './dev/tracing.js';
3843

3944
const FLUSH_MICROTASK = 0;
4045
const FLUSH_SYNC = 1;
@@ -739,9 +744,6 @@ export function get(signal) {
739744

740745
// Register the dependency on the current reaction signal.
741746
if (active_reaction !== null) {
742-
if (DEV && tracing_expressions !== null) {
743-
tracing_reactive_signal();
744-
}
745747
if (derived_sources !== null && derived_sources.includes(signal)) {
746748
e.state_unsafe_local_read();
747749
}
@@ -785,6 +787,12 @@ export function get(signal) {
785787
}
786788
}
787789

790+
if (DEV && active_reaction !== null && tracing_expressions !== null) {
791+
set_tracing_expression_reactive(
792+
signal.version > active_reaction.version ? REACTIVE_CHANGED : REACTIVE_UNCHANGED
793+
);
794+
}
795+
788796
return signal.v;
789797
}
790798

0 commit comments

Comments
 (0)