@@ -15,20 +15,48 @@ import type {
1515 TraceEventContainer ,
1616} from './trace-file.type.js' ;
1717
18- // eslint-disable-next-line functional/no-let
18+ /** Global counter for generating unique local IDs */
1919let id2Count = 0 ;
20+
21+ /**
22+ * Generates a unique local ID for span events, to link start and end with a id.
23+ * @returns Object with local ID string
24+ */
2025export const nextId2 = ( ) => ( { local : `0x${ ++ id2Count } ` } ) ;
2126
27+ /**
28+ * Provides default values for trace event properties.
29+ * @param opt - Optional overrides for pid, tid, and timestamp
30+ * @returns Object with pid, tid, and timestamp
31+ */
2232const defaults = ( opt ?: { pid ?: number ; tid ?: number ; ts ?: number } ) => ( {
2333 pid : opt ?. pid ?? process . pid ,
2434 tid : opt ?. tid ?? threadId ,
2535 ts : opt ?. ts ?? defaultClock . epochNowUs ( ) ,
2636} ) ;
2737
38+ /**
39+ * Generates a unique frame tree node ID from process and thread IDs.
40+ * @param pid - Process ID
41+ * @param tid - Thread ID
42+ * @returns Combined numeric ID
43+ */
2844export const frameTreeNodeId = ( pid : number , tid : number ) =>
2945 Number . parseInt ( `${ pid } 0${ tid } ` , 10 ) ;
46+
47+ /**
48+ * Generates a frame name string from process and thread IDs.
49+ * @param pid - Process ID
50+ * @param tid - Thread ID
51+ * @returns Formatted frame name
52+ */
3053export const frameName = ( pid : number , tid : number ) => `FRAME0P${ pid } T${ tid } ` ;
3154
55+ /**
56+ * Creates an instant trace event for marking a point in time.
57+ * @param opt - Event configuration options
58+ * @returns InstantEvent object
59+ */
3260export const getInstantEvent = ( opt : {
3361 name : string ;
3462 ts ?: number ;
@@ -43,6 +71,12 @@ export const getInstantEvent = (opt: {
4371 args : opt . args ?? { } ,
4472} ) ;
4573
74+ /**
75+ * Creates a start tracing event with frame information.
76+ * This event is needed to make events in general show up and also colores the track better.
77+ * @param opt - Tracing configuration options
78+ * @returns StartTracingEvent object
79+ */
4680export const getStartTracing = ( opt : {
4781 url : string ;
4882 ts ?: number ;
@@ -78,6 +112,11 @@ export const getStartTracing = (opt: {
78112 } ;
79113} ;
80114
115+ /**
116+ * Creates a complete trace event with duration.
117+ * @param opt - Event configuration with name and duration
118+ * @returns CompleteEvent object
119+ */
81120export const getCompleteEvent = ( opt : {
82121 name : string ;
83122 dur : number ;
@@ -93,6 +132,7 @@ export const getCompleteEvent = (opt: {
93132 args : { } ,
94133} ) ;
95134
135+ /** Options for creating span events */
96136type SpanOpt = {
97137 name : string ;
98138 id2 : { local : string } ;
@@ -102,8 +142,26 @@ type SpanOpt = {
102142 args ?: SpanEventArgs ;
103143} ;
104144
145+ /**
146+ * Creates a begin span event.
147+ * @param ph - Phase ('b' for begin)
148+ * @param opt - Span event options
149+ * @returns BeginEvent object
150+ */
105151export function getSpanEvent ( ph : 'b' , opt : SpanOpt ) : BeginEvent ;
152+ /**
153+ * Creates an end span event.
154+ * @param ph - Phase ('e' for end)
155+ * @param opt - Span event options
156+ * @returns EndEvent object
157+ */
106158export function getSpanEvent ( ph : 'e' , opt : SpanOpt ) : EndEvent ;
159+ /**
160+ * Creates a span event (begin or end).
161+ * @param ph - Phase ('b' or 'e')
162+ * @param opt - Span event options
163+ * @returns SpanEvent object
164+ */
107165export function getSpanEvent ( ph : 'b' | 'e' , opt : SpanOpt ) : SpanEvent {
108166 return {
109167 cat : 'blink.user_timing' ,
@@ -117,6 +175,11 @@ export function getSpanEvent(ph: 'b' | 'e', opt: SpanOpt): SpanEvent {
117175 } ;
118176}
119177
178+ /**
179+ * Creates a pair of begin and end span events.
180+ * @param opt - Span configuration with start/end timestamps
181+ * @returns Tuple of BeginEvent and EndEvent
182+ */
120183export const getSpan = ( opt : {
121184 name : string ;
122185 tsB : number ;
@@ -150,6 +213,12 @@ export const getSpan = (opt: {
150213 ] ;
151214} ;
152215
216+ /**
217+ * Converts a PerformanceMark to an instant trace event.
218+ * @param entry - Performance mark entry
219+ * @param opt - Optional overrides for name, pid, and tid
220+ * @returns InstantEvent object
221+ */
153222export const markToInstantEvent = (
154223 entry : PerformanceMark ,
155224 opt ?: { name ?: string ; pid ?: number ; tid ?: number } ,
@@ -161,6 +230,12 @@ export const markToInstantEvent = (
161230 args : entry . detail ? { detail : entry . detail } : undefined ,
162231 } ) ;
163232
233+ /**
234+ * Converts a PerformanceMeasure to a pair of span events.
235+ * @param entry - Performance measure entry
236+ * @param opt - Optional overrides for name, pid, and tid
237+ * @returns Tuple of BeginEvent and EndEvent
238+ */
164239export const measureToSpanEvents = (
165240 entry : PerformanceMeasure ,
166241 opt ?: { name ?: string ; pid ?: number ; tid ?: number } ,
@@ -173,6 +248,11 @@ export const measureToSpanEvents = (
173248 args : entry . detail ? { data : { detail : entry . detail } } : undefined ,
174249 } ) ;
175250
251+ /**
252+ * Creates a complete trace file container with metadata.
253+ * @param opt - Trace file configuration
254+ * @returns TraceEventContainer with events and metadata
255+ */
176256export const getTraceFile = ( opt : {
177257 traceEvents : TraceEvent [ ] ;
178258 startTime ?: string ;
0 commit comments