@@ -37,15 +37,21 @@ export function get(event: Types.Events.Event, parsedTrace: Handlers.Types.Parse
3737 result = getForPerformanceMeasure ( event , parsedTrace ) ;
3838 } else {
3939 result = getForEvent ( event , parsedTrace ) ;
40- const maybeProtocolCallFrame = getPayloadStackAsProtocolCallFrame ( event ) ;
41- if ( result && maybeProtocolCallFrame && ! isNativeJSFunction ( maybeProtocolCallFrame ) ) {
42- // If the event has a payload stack trace, replace the top frame
43- // of the calculated stack with the top frame of the payload stack
44- // because trace payload call frames contain call locations, unlike
45- // profile call frames (which contain function declaration locations).
46- // This way the user knows which exact JS location triggered an
47- // event.
48- result . callFrames [ 0 ] = maybeProtocolCallFrame ;
40+ const payloadCallFrames =
41+ getTraceEventPayloadStackAsProtocolCallFrame ( event ) . filter ( callFrame => ! isNativeJSFunction ( callFrame ) ) ;
42+ // If the event has a payload stack trace, replace the synchronous
43+ // portion of the calculated stack with the payload's call frames.
44+ // We do this because trace payload call frames contain call
45+ // locations, unlike profile call frames obtained with getForEvent
46+ // (which contain function declaration locations).
47+ // This way the user knows which exact JS location triggered an
48+ // event.
49+ if ( ! result . callFrames . length ) {
50+ result . callFrames = payloadCallFrames ;
51+ } else {
52+ for ( let i = 0 ; i < payloadCallFrames . length && i < result . callFrames . length ; i ++ ) {
53+ result . callFrames [ i ] = payloadCallFrames [ i ] ;
54+ }
4955 }
5056 }
5157 if ( result ) {
@@ -187,13 +193,14 @@ function isNativeJSFunction({columnNumber, lineNumber, url, scriptId}: Protocol.
187193}
188194
189195/**
190- * Extracts the top frame in the stack contained in a trace event's payload
191- * (if any) and casts it as a Protocol.Runtime.CallFrame.
196+ * Converts a stack trace from a trace event's payload into an array of
197+ * Protocol.Runtime.CallFrame.
192198 */
193- function getPayloadStackAsProtocolCallFrame ( event : Types . Events . Event ) : Protocol . Runtime . CallFrame | null {
194- const maybeCallStack = Helpers . Trace . getZeroIndexedStackTraceInEventPayload ( event ) ;
195- const maybeCallFrame = maybeCallStack ?. at ( 0 ) ;
196- const maybeProtocolCallFrame =
197- maybeCallFrame && { ...maybeCallFrame , scriptId : String ( maybeCallFrame . scriptId ) as Protocol . Runtime . ScriptId } ;
198- return maybeProtocolCallFrame || null ;
199+ function getTraceEventPayloadStackAsProtocolCallFrame ( event : Types . Events . Event ) : Protocol . Runtime . CallFrame [ ] {
200+ const payloadCallStack = Helpers . Trace . getZeroIndexedStackTraceInEventPayload ( event ) || [ ] ;
201+ const callFrames : Protocol . Runtime . CallFrame [ ] = [ ] ;
202+ for ( const frame of payloadCallStack ) {
203+ callFrames . push ( { ...frame , scriptId : String ( frame . scriptId ) as Protocol . Runtime . ScriptId } ) ;
204+ }
205+ return callFrames ;
199206}
0 commit comments