Skip to content

Commit 68a68ea

Browse files
gabrieltrompizzhengjitf
authored andcommitted
Allow to use the Profiler when no client is connected in standalone DevTools (facebook#22551)
1 parent 22534e0 commit 68a68ea

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

packages/react-devtools-core/src/standalone.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ import {readFileSync} from 'fs';
3030
import {installHook} from 'react-devtools-shared/src/hook';
3131
import DevTools from 'react-devtools-shared/src/devtools/views/DevTools';
3232
import {doesFilePathExist, launchEditor} from './editor';
33-
import {__DEBUG__} from 'react-devtools-shared/src/constants';
33+
import {
34+
__DEBUG__,
35+
LOCAL_STORAGE_DEFAULT_TAB_KEY,
36+
} from 'react-devtools-shared/src/constants';
37+
import {localStorageSetItem} from '../../react-devtools-shared/src/storage';
3438

3539
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
3640
import type {InspectedElement} from 'react-devtools-shared/src/devtools/views/Components/types';
@@ -179,6 +183,20 @@ function onError({code, message}) {
179183
}
180184
}
181185

186+
function openProfiler() {
187+
// Mocked up bridge and store to allow the DevTools to be rendered
188+
bridge = new Bridge({listen: () => {}, send: () => {}});
189+
store = new Store(bridge, {});
190+
191+
// Ensure the Profiler tab is shown initially.
192+
localStorageSetItem(
193+
LOCAL_STORAGE_DEFAULT_TAB_KEY,
194+
JSON.stringify('profiler'),
195+
);
196+
197+
reload();
198+
}
199+
182200
function initialize(socket: WebSocket) {
183201
const listeners = [];
184202
socket.onmessage = event => {
@@ -372,6 +390,7 @@ const DevtoolsUI = {
372390
setProjectRoots,
373391
setStatusListener,
374392
startServer,
393+
openProfiler,
375394
};
376395

377396
export default DevtoolsUI;

packages/react-devtools-shared/src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const TREE_OPERATION_UPDATE_TREE_BASE_DURATION = 4;
2020
export const TREE_OPERATION_UPDATE_ERRORS_OR_WARNINGS = 5;
2121
export const TREE_OPERATION_REMOVE_ROOT = 6;
2222

23+
export const LOCAL_STORAGE_DEFAULT_TAB_KEY = 'React::DevTools::defaultTab';
24+
2325
export const LOCAL_STORAGE_FILTER_PREFERENCES_KEY =
2426
'React::DevTools::componentFilters';
2527

packages/react-devtools-shared/src/devtools/views/DevTools.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import UnsupportedVersionDialog from './UnsupportedVersionDialog';
3838
import WarnIfLegacyBackendDetected from './WarnIfLegacyBackendDetected';
3939
import {useLocalStorage} from './hooks';
4040
import ThemeProvider from './ThemeProvider';
41+
import {LOCAL_STORAGE_DEFAULT_TAB_KEY} from '../../constants';
4142

4243
import styles from './DevTools.css';
4344

@@ -143,7 +144,7 @@ export default function DevTools({
143144
hideViewSourceAction,
144145
}: Props) {
145146
const [currentTab, setTab] = useLocalStorage<TabID>(
146-
'React::DevTools::defaultTab',
147+
LOCAL_STORAGE_DEFAULT_TAB_KEY,
147148
defaultTab,
148149
);
149150

packages/react-devtools/app.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@
142142
<strong>before</strong> importing React DOM.
143143
</div>
144144
</div>
145+
<div class="box">
146+
<div class="box-header">Profiler</div>
147+
<div class="box-content">
148+
Open the <a
149+
id="profiler"
150+
class="link"
151+
href="#"
152+
>Profiler tab</a> to inspect saved profiles.
153+
</div>
154+
</div>
145155
<div id="loading-status">Starting the server…</div>
146156
</div>
147157
</div>
@@ -201,6 +211,12 @@
201211
}
202212
}
203213

214+
function openProfiler() {
215+
window.devtools
216+
.setContentDOMNode(document.getElementById("container"))
217+
.openProfiler();
218+
}
219+
204220
const link = $('#rn-help-link');
205221
link.addEventListener('click', event => {
206222
event.preventDefault();
@@ -217,6 +233,9 @@
217233
$byIp.addEventListener('click', selectAllAndCopy);
218234
$byIp.addEventListener('focus', selectAllAndCopy);
219235

236+
const $profiler = $("#profiler");
237+
$profiler.addEventListener('click', openProfiler);
238+
220239
let devtools;
221240
try {
222241
devtools = require("react-devtools-core/standalone").default;

0 commit comments

Comments
 (0)