@@ -12,7 +12,8 @@ import type {LoggerEvent} from 'react-devtools-shared/src/Logger';
12
12
import { registerEventLogger } from 'react-devtools-shared/src/Logger' ;
13
13
import { enableLogger } from 'react-devtools-feature-flags' ;
14
14
15
- let loggingIFrame = null ;
15
+ let currentLoggingIFrame = null ;
16
+ let currentSessionId = null ;
16
17
let missedEvents : Array < LoggerEvent > = [ ] ;
17
18
18
19
type LoggerContext = {
@@ -21,32 +22,27 @@ type LoggerContext = {
21
22
22
23
export function registerDevToolsEventLogger (
23
24
surface : string ,
24
- fetchAdditionalContext : ? ( ) = >
25
- | LoggerContext
26
- | ? ( ( ) => Promise < LoggerContext > ) ,
25
+ fetchAdditionalContext ?:
26
+ | ( ( ) => LoggerContext )
27
+ | ( ( ) => Promise < LoggerContext > ) ,
27
28
) : void {
28
29
async function logEvent ( event : LoggerEvent ) {
29
30
if ( enableLogger ) {
30
- if ( loggingIFrame != null ) {
31
- let metadata = null ;
32
- if ( event . metadata != null ) {
33
- metadata = event . metadata ;
34
- // $FlowFixMe[cannot-write]: metadata is not writable and nullable
35
- // $FlowFixMe[prop-missing]
36
- delete event . metadata ;
37
- }
38
- loggingIFrame . contentWindow . postMessage (
31
+ if ( currentLoggingIFrame != null && currentSessionId != null ) {
32
+ const { metadata, ...eventWithoutMetadata } = event ;
33
+ const additionalContext : LoggerContext | { } =
34
+ fetchAdditionalContext != null ? await fetchAdditionalContext ( ) : { } ;
35
+
36
+ currentLoggingIFrame ?. contentWindow ?. postMessage (
39
37
{
40
38
source : 'react-devtools-logging' ,
41
- event : event ,
39
+ event : eventWithoutMetadata ,
42
40
context : {
41
+ ...additionalContext ,
42
+ metadata : metadata != null ? JSON . stringify ( metadata ) : '' ,
43
+ session_id : currentSessionId ,
43
44
surface,
44
45
version : process . env . DEVTOOLS_VERSION ,
45
- metadata : metadata !== null ? JSON . stringify ( metadata ) : '' ,
46
- ...( fetchAdditionalContext != null
47
- ? // $FlowFixMe[not-an-object]
48
- await fetchAdditionalContext ( )
49
- : { } ) ,
50
46
} ,
51
47
} ,
52
48
'*' ,
@@ -58,11 +54,8 @@ export function registerDevToolsEventLogger(
58
54
}
59
55
60
56
function handleLoggingIFrameLoaded ( iframe : HTMLIFrameElement ) {
61
- if ( loggingIFrame != null ) {
62
- return ;
63
- }
57
+ currentLoggingIFrame = iframe ;
64
58
65
- loggingIFrame = iframe ;
66
59
if ( missedEvents . length > 0 ) {
67
60
missedEvents . forEach ( event => logEvent ( event ) ) ;
68
61
missedEvents = [ ] ;
@@ -74,18 +67,21 @@ export function registerDevToolsEventLogger(
74
67
if ( enableLogger ) {
75
68
const loggingUrl = process . env . LOGGING_URL ;
76
69
const body = document . body ;
70
+
77
71
if (
78
72
typeof loggingUrl === 'string' &&
79
73
loggingUrl . length > 0 &&
80
- body != null
74
+ body != null &&
75
+ currentLoggingIFrame == null
81
76
) {
82
77
registerEventLogger ( logEvent ) ;
78
+ currentSessionId = window . crypto . randomUUID ( ) ;
83
79
84
80
const iframe = document . createElement ( 'iframe' ) ;
81
+
82
+ iframe . onload = ( ) => handleLoggingIFrameLoaded ( iframe ) ;
85
83
iframe . src = loggingUrl ;
86
- iframe . onload = function ( ...args ) {
87
- handleLoggingIFrameLoaded ( iframe ) ;
88
- } ;
84
+
89
85
body . appendChild ( iframe ) ;
90
86
}
91
87
}
0 commit comments