forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregisterDevToolsEventLogger.js
More file actions
71 lines (63 loc) · 1.78 KB
/
registerDevToolsEventLogger.js
File metadata and controls
71 lines (63 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
*/
import type {LogEvent} from 'react-devtools-shared/src/Logger';
import {registerEventLogger} from 'react-devtools-shared/src/Logger';
import {enableLogger} from 'react-devtools-feature-flags';
let loggingIFrame = null;
let missedEvents = [];
export function registerDevToolsEventLogger(surface: string) {
function logEvent(event: LogEvent) {
if (enableLogger) {
if (loggingIFrame != null) {
loggingIFrame.contentWindow.postMessage(
{
source: 'react-devtools-logging',
event: event,
context: {
surface,
version: process.env.DEVTOOLS_VERSION,
},
},
'*',
);
} else {
missedEvents.push(event);
}
}
}
function handleLoggingIFrameLoaded(iframe) {
if (loggingIFrame != null) {
return;
}
loggingIFrame = iframe;
if (missedEvents.length > 0) {
missedEvents.forEach(logEvent);
missedEvents = [];
}
}
// If logger is enabled, register a logger that captures logged events
// and render iframe where the logged events will be reported to
if (enableLogger) {
const loggingUrl = process.env.LOGGING_URL;
const body = document.body;
if (
typeof loggingUrl === 'string' &&
loggingUrl.length > 0 &&
body != null
) {
registerEventLogger(logEvent);
const iframe = document.createElement('iframe');
iframe.src = loggingUrl;
iframe.onload = function(...args) {
handleLoggingIFrameLoaded(iframe);
};
body.appendChild(iframe);
}
}
}