Skip to content

Commit

Permalink
Allow to use the Profiler when no client is connected in standalone D…
Browse files Browse the repository at this point in the history
…evTools (facebook#22551)
  • Loading branch information
gabrieltrompiz authored and zhengjitf committed Apr 15, 2022
1 parent 22534e0 commit 68a68ea
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
21 changes: 20 additions & 1 deletion packages/react-devtools-core/src/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import {readFileSync} from 'fs';
import {installHook} from 'react-devtools-shared/src/hook';
import DevTools from 'react-devtools-shared/src/devtools/views/DevTools';
import {doesFilePathExist, launchEditor} from './editor';
import {__DEBUG__} from 'react-devtools-shared/src/constants';
import {
__DEBUG__,
LOCAL_STORAGE_DEFAULT_TAB_KEY,
} from 'react-devtools-shared/src/constants';
import {localStorageSetItem} from '../../react-devtools-shared/src/storage';

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

function openProfiler() {
// Mocked up bridge and store to allow the DevTools to be rendered
bridge = new Bridge({listen: () => {}, send: () => {}});
store = new Store(bridge, {});

// Ensure the Profiler tab is shown initially.
localStorageSetItem(
LOCAL_STORAGE_DEFAULT_TAB_KEY,
JSON.stringify('profiler'),
);

reload();
}

function initialize(socket: WebSocket) {
const listeners = [];
socket.onmessage = event => {
Expand Down Expand Up @@ -372,6 +390,7 @@ const DevtoolsUI = {
setProjectRoots,
setStatusListener,
startServer,
openProfiler,
};

export default DevtoolsUI;
2 changes: 2 additions & 0 deletions packages/react-devtools-shared/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const TREE_OPERATION_UPDATE_TREE_BASE_DURATION = 4;
export const TREE_OPERATION_UPDATE_ERRORS_OR_WARNINGS = 5;
export const TREE_OPERATION_REMOVE_ROOT = 6;

export const LOCAL_STORAGE_DEFAULT_TAB_KEY = 'React::DevTools::defaultTab';

export const LOCAL_STORAGE_FILTER_PREFERENCES_KEY =
'React::DevTools::componentFilters';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import UnsupportedVersionDialog from './UnsupportedVersionDialog';
import WarnIfLegacyBackendDetected from './WarnIfLegacyBackendDetected';
import {useLocalStorage} from './hooks';
import ThemeProvider from './ThemeProvider';
import {LOCAL_STORAGE_DEFAULT_TAB_KEY} from '../../constants';

import styles from './DevTools.css';

Expand Down Expand Up @@ -143,7 +144,7 @@ export default function DevTools({
hideViewSourceAction,
}: Props) {
const [currentTab, setTab] = useLocalStorage<TabID>(
'React::DevTools::defaultTab',
LOCAL_STORAGE_DEFAULT_TAB_KEY,
defaultTab,
);

Expand Down
19 changes: 19 additions & 0 deletions packages/react-devtools/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@
<strong>before</strong> importing React DOM.
</div>
</div>
<div class="box">
<div class="box-header">Profiler</div>
<div class="box-content">
Open the <a
id="profiler"
class="link"
href="#"
>Profiler tab</a> to inspect saved profiles.
</div>
</div>
<div id="loading-status">Starting the server…</div>
</div>
</div>
Expand Down Expand Up @@ -201,6 +211,12 @@
}
}

function openProfiler() {
window.devtools
.setContentDOMNode(document.getElementById("container"))
.openProfiler();
}

const link = $('#rn-help-link');
link.addEventListener('click', event => {
event.preventDefault();
Expand All @@ -217,6 +233,9 @@
$byIp.addEventListener('click', selectAllAndCopy);
$byIp.addEventListener('focus', selectAllAndCopy);

const $profiler = $("#profiler");
$profiler.addEventListener('click', openProfiler);

let devtools;
try {
devtools = require("react-devtools-core/standalone").default;
Expand Down

0 comments on commit 68a68ea

Please sign in to comment.