Skip to content

Commit

Permalink
[react devtools][easy] Centralize calls to patchConsoleUsingWindowVal…
Browse files Browse the repository at this point in the history
…ues (#25222)

* Instead of reading from window in two separate places, do this in a single function
* Add some type safety
  • Loading branch information
rbalicki2 authored and rickhanlonii committed Oct 5, 2022
1 parent cf24a1c commit 6a9ac8d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
34 changes: 34 additions & 0 deletions packages/react-devtools-shared/src/backend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,37 @@ export function unpatchForStrictMode(): void {
}
}
}

export function patchConsoleUsingWindowValues() {
const appendComponentStack =
castBool(window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__) ?? true;
const breakOnConsoleErrors =
castBool(window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__) ?? false;
const showInlineWarningsAndErrors =
castBool(window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__) ?? true;
const hideConsoleLogsInStrictMode =
castBool(window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__) ??
false;
const browserTheme =
castBrowserTheme(window.__REACT_DEVTOOLS_BROWSER_THEME__) ?? 'dark';

patch({
appendComponentStack,
breakOnConsoleErrors,
showInlineWarningsAndErrors,
hideConsoleLogsInStrictMode,
browserTheme,
});
}

function castBool(v: any): ?boolean {
if (v === true || v === false) {
return v;
}
}

function castBrowserTheme(v: any): ?BrowserTheme {
if (v === 'light' || v === 'dark' || v === 'auto') {
return v;
}
}
20 changes: 2 additions & 18 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
} from '../constants';
import {inspectHooksOfFiber} from 'react-debug-tools';
import {
patch as patchConsole,
patchConsoleUsingWindowValues,
registerRenderer as registerRendererWithConsole,
patchForStrictMode as patchConsoleForStrictMode,
unpatchForStrictMode as unpatchConsoleForStrictMode,
Expand Down Expand Up @@ -817,23 +817,7 @@ export function attach(
// The renderer interface can't read these preferences directly,
// because it is stored in localStorage within the context of the extension.
// It relies on the extension to pass the preference through via the global.
const appendComponentStack =
window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ !== false;
const breakOnConsoleErrors =
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ === true;
const showInlineWarningsAndErrors =
window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ !== false;
const hideConsoleLogsInStrictMode =
window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ === true;
const browserTheme = window.__REACT_DEVTOOLS_BROWSER_THEME__;

patchConsole({
appendComponentStack,
breakOnConsoleErrors,
showInlineWarningsAndErrors,
hideConsoleLogsInStrictMode,
browserTheme,
});
patchConsoleUsingWindowValues();

const debug = (
name: string,
Expand Down
20 changes: 2 additions & 18 deletions packages/react-devtools-shared/src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevToo
import type {DevToolsHook} from 'react-devtools-shared/src/backend/types';

import {
patch as patchConsole,
patchConsoleUsingWindowValues,
registerRenderer as registerRendererWithConsole,
} from './backend/console';

Expand Down Expand Up @@ -343,16 +343,6 @@ export function installHook(target: any): DevToolsHook | null {
// (See comments in the try/catch below for more context on inlining.)
if (!__TEST__ && !__EXTENSION__) {
try {
const appendComponentStack =
window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ !== false;
const breakOnConsoleErrors =
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ === true;
const showInlineWarningsAndErrors =
window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ !== false;
const hideConsoleLogsInStrictMode =
window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ === true;
const browserTheme = window.__REACT_DEVTOOLS_BROWSER_THEME__;

// The installHook() function is injected by being stringified in the browser,
// so imports outside of this function do not get included.
//
Expand All @@ -361,13 +351,7 @@ export function installHook(target: any): DevToolsHook | null {
// and the object itself will be undefined as well for the reasons mentioned above,
// so we use try/catch instead.
registerRendererWithConsole(renderer);
patchConsole({
appendComponentStack,
breakOnConsoleErrors,
showInlineWarningsAndErrors,
hideConsoleLogsInStrictMode,
browserTheme,
});
patchConsoleUsingWindowValues();
} catch (error) {}
}

Expand Down

0 comments on commit 6a9ac8d

Please sign in to comment.