Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use the Profiler when no client is connected in standalone DevTools #22551

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
gabrieltrompiz marked this conversation as resolved.
Show resolved Hide resolved
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();
gabrieltrompiz marked this conversation as resolved.
Show resolved Hide resolved
}

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