@@ -6,40 +6,26 @@ import { DERIVED, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
6
6
import { effect_tracking } from '../reactivity/effects.js' ;
7
7
import { active_reaction , captured_signals , set_captured_signals , untrack } from '../runtime.js' ;
8
8
9
- /** @type { any } */
9
+ /**
10
+ * @typedef {{
11
+ * traces: Error[];
12
+ * }} TraceEntry
13
+ */
14
+
15
+ /** @type {{ reaction: Reaction | null, entries: Map<Value, TraceEntry> } | null } */
10
16
export let tracing_expressions = null ;
11
17
12
18
/**
13
- * @param { Value } signal
14
- * @param { { read: Error[] } } [entry]
19
+ * @param {Value } signal
20
+ * @param {TraceEntry } [entry]
15
21
*/
16
22
function log_entry ( signal , entry ) {
17
- const debug = signal . debug ;
18
- const value = signal . trace_need_increase ? signal . trace_v : signal . v ;
23
+ const value = signal . v ;
19
24
20
25
if ( value === UNINITIALIZED ) {
21
26
return ;
22
27
}
23
28
24
- if ( debug ) {
25
- var previous_captured_signals = captured_signals ;
26
- var captured = new Set ( ) ;
27
- set_captured_signals ( captured ) ;
28
- try {
29
- untrack ( ( ) => {
30
- debug ( ) ;
31
- } ) ;
32
- } finally {
33
- set_captured_signals ( previous_captured_signals ) ;
34
- }
35
- if ( captured . size > 0 ) {
36
- for ( const dep of captured ) {
37
- log_entry ( dep ) ;
38
- }
39
- return ;
40
- }
41
- }
42
-
43
29
const type = ( signal . f & DERIVED ) !== 0 ? '$derived' : '$state' ;
44
30
const current_reaction = /** @type {Reaction } */ ( active_reaction ) ;
45
31
const dirty = signal . wv > current_reaction . wv || current_reaction . wv === 0 ;
@@ -69,17 +55,15 @@ function log_entry(signal, entry) {
69
55
console . log ( signal . created ) ;
70
56
}
71
57
72
- if ( signal . updated ) {
58
+ if ( dirty && signal . updated ) {
73
59
// eslint-disable-next-line no-console
74
60
console . log ( signal . updated ) ;
75
61
}
76
62
77
- const read = entry ?. read ;
78
-
79
- if ( read && read . length > 0 ) {
80
- for ( var stack of read ) {
63
+ if ( entry ) {
64
+ for ( var trace of entry . traces ) {
81
65
// eslint-disable-next-line no-console
82
- console . log ( stack ) ;
66
+ console . log ( trace ) ;
83
67
}
84
68
}
85
69
@@ -94,46 +78,40 @@ function log_entry(signal, entry) {
94
78
*/
95
79
export function trace ( label , fn ) {
96
80
var previously_tracing_expressions = tracing_expressions ;
81
+
97
82
try {
98
83
tracing_expressions = { entries : new Map ( ) , reaction : active_reaction } ;
99
84
100
85
var start = performance . now ( ) ;
101
86
var value = fn ( ) ;
102
87
var time = ( performance . now ( ) - start ) . toFixed ( 2 ) ;
103
88
89
+ var prefix = untrack ( label ) ;
90
+
104
91
if ( ! effect_tracking ( ) ) {
105
92
// eslint-disable-next-line no-console
106
- console . log ( `${ label ( ) } %cran outside of an effect (${ time } ms)` , 'color: grey' ) ;
93
+ console . log ( `${ prefix } %cran outside of an effect (${ time } ms)` , 'color: grey' ) ;
107
94
} else if ( tracing_expressions . entries . size === 0 ) {
108
95
// eslint-disable-next-line no-console
109
- console . log ( `${ label ( ) } %cno reactive dependencies (${ time } ms)` , 'color: grey' ) ;
96
+ console . log ( `${ prefix } %cno reactive dependencies (${ time } ms)` , 'color: grey' ) ;
110
97
} else {
111
98
// eslint-disable-next-line no-console
112
- console . group ( `${ label ( ) } %c(${ time } ms)` , 'color: grey' ) ;
99
+ console . group ( `${ prefix } %c(${ time } ms)` , 'color: grey' ) ;
113
100
114
101
var entries = tracing_expressions . entries ;
115
102
103
+ untrack ( ( ) => {
104
+ for ( const [ signal , traces ] of entries ) {
105
+ log_entry ( signal , traces ) ;
106
+ }
107
+ } ) ;
108
+
116
109
tracing_expressions = null ;
117
110
118
- for ( const [ signal , entry ] of entries ) {
119
- log_entry ( signal , entry ) ;
120
- }
121
111
// eslint-disable-next-line no-console
122
112
console . groupEnd ( ) ;
123
113
}
124
114
125
- if ( previously_tracing_expressions !== null && tracing_expressions !== null ) {
126
- for ( const [ signal , entry ] of tracing_expressions . entries ) {
127
- var prev_entry = previously_tracing_expressions . get ( signal ) ;
128
-
129
- if ( prev_entry === undefined ) {
130
- previously_tracing_expressions . set ( signal , entry ) ;
131
- } else {
132
- prev_entry . read . push ( ...entry . read ) ;
133
- }
134
- }
135
- }
136
-
137
115
return value ;
138
116
} finally {
139
117
tracing_expressions = previously_tracing_expressions ;
0 commit comments