@@ -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,24 @@ type LoggerContext = {
21
22
22
23
export function registerDevToolsEventLogger (
23
24
surface : string ,
24
- fetchAdditionalContext : ?( ) = >
25
- | LoggerContext
26
- | ?( ( ) => Promise < LoggerContext > ) ,
25
+ fetchAdditionalContext ?: ( ( ) => LoggerContext ) | ( ( ) => Promise < LoggerContext > ) ,
27
26
) : void {
28
27
async function logEvent ( event : LoggerEvent ) {
29
28
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 (
29
+ if ( currentLoggingIFrame != null && currentSessionId != null ) {
30
+ const { metadata, ...eventWithoutMetadata } = event ;
31
+ const additionalContext : LoggerContext | { } = fetchAdditionalContext != null ? await fetchAdditionalContext ( ) : { } ;
32
+
33
+ currentLoggingIFrame ?. contentWindow ?. postMessage (
39
34
{
40
35
source : 'react-devtools-logging' ,
41
- event : event ,
36
+ event : eventWithoutMetadata ,
42
37
context : {
38
+ ...additionalContext ,
39
+ metadata : metadata != null ? JSON . stringify ( metadata ) : '' ,
40
+ session_id : currentSessionId ,
43
41
surface,
44
42
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
43
} ,
51
44
} ,
52
45
'*' ,
@@ -58,11 +51,8 @@ export function registerDevToolsEventLogger(
58
51
}
59
52
60
53
function handleLoggingIFrameLoaded ( iframe : HTMLIFrameElement ) {
61
- if ( loggingIFrame != null ) {
62
- return ;
63
- }
54
+ currentLoggingIFrame = iframe ;
64
55
65
- loggingIFrame = iframe ;
66
56
if ( missedEvents . length > 0 ) {
67
57
missedEvents . forEach ( event => logEvent ( event ) ) ;
68
58
missedEvents = [ ] ;
@@ -74,18 +64,21 @@ export function registerDevToolsEventLogger(
74
64
if ( enableLogger ) {
75
65
const loggingUrl = process . env . LOGGING_URL ;
76
66
const body = document . body ;
67
+
77
68
if (
78
69
typeof loggingUrl === 'string' &&
79
70
loggingUrl . length > 0 &&
80
- body != null
71
+ body != null &&
72
+ currentLoggingIFrame == null
81
73
) {
82
74
registerEventLogger ( logEvent ) ;
75
+ currentSessionId = window . crypto . randomUUID ( ) ;
83
76
84
77
const iframe = document . createElement ( 'iframe' ) ;
78
+
79
+ iframe . onload = ( ) => handleLoggingIFrameLoaded ( iframe ) ;
85
80
iframe . src = loggingUrl ;
86
- iframe . onload = function ( ...args ) {
87
- handleLoggingIFrameLoaded ( iframe ) ;
88
- } ;
81
+
89
82
body . appendChild ( iframe ) ;
90
83
}
91
84
}
0 commit comments