Skip to content

Commit

Permalink
feat[react-devtools]: add settings to global hook object
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxyq committed Sep 10, 2024
1 parent 3dfd5d9 commit 09a7a6a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
39 changes: 15 additions & 24 deletions packages/react-devtools-shared/src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ import type {
RendererID,
RendererInterface,
ConsolePatchSettings,
DevToolsHookSettings,
} from './types';
import type {
ComponentFilter,
BrowserTheme,
} from 'react-devtools-shared/src/frontend/types';
import type {ComponentFilter} from 'react-devtools-shared/src/frontend/types';
import {isSynchronousXHRSupported, isReactNativeEnvironment} from './utils';

const debug = (methodName: string, ...args: Array<string>) => {
Expand Down Expand Up @@ -152,6 +150,7 @@ export default class Agent extends EventEmitter<{
traceUpdates: [Set<HostInstance>],
drawTraceUpdates: [Array<HostInstance>],
disableTraceUpdates: [],
updateHookSettings: [DevToolsHookSettings],
}> {
_bridge: BackendBridge;
_isProfiling: boolean = false;
Expand Down Expand Up @@ -805,30 +804,22 @@ export default class Agent extends EventEmitter<{
}
};

updateConsolePatchSettings: ({
appendComponentStack: boolean,
breakOnConsoleErrors: boolean,
browserTheme: BrowserTheme,
hideConsoleLogsInStrictMode: boolean,
showInlineWarningsAndErrors: boolean,
}) => void = ({
appendComponentStack,
breakOnConsoleErrors,
showInlineWarningsAndErrors,
hideConsoleLogsInStrictMode,
browserTheme,
}: ConsolePatchSettings) => {
updateConsolePatchSettings: (
settings: $ReadOnly<ConsolePatchSettings>,
) => void = settings => {
// Propagate the settings, so Backend can subscribe to it and modify hook
this.emit('updateHookSettings', {
appendComponentStack: settings.appendComponentStack,
breakOnConsoleErrors: settings.breakOnConsoleErrors,
showInlineWarningsAndErrors: settings.showInlineWarningsAndErrors,
hideConsoleLogsInStrictMode: settings.hideConsoleLogsInStrictMode,
});

// If the frontend preferences have changed,
// or in the case of React Native- if the backend is just finding out the preferences-
// then reinstall the console overrides.
// It's safe to call `patchConsole` multiple times.
patchConsole({
appendComponentStack,
breakOnConsoleErrors,
showInlineWarningsAndErrors,
hideConsoleLogsInStrictMode,
browserTheme,
});
patchConsole(settings);
};

updateComponentFilters: (componentFilters: Array<ComponentFilter>) => void =
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shared/src/backend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function patch({
showInlineWarningsAndErrors,
hideConsoleLogsInStrictMode,
browserTheme,
}: ConsolePatchSettings): void {
}: $ReadOnly<ConsolePatchSettings>): void {
// Settings may change after we've patched the console.
// Using a shared ref allows the patch function to read the latest values.
consoleSettingsRef.appendComponentStack = appendComponentStack;
Expand Down
4 changes: 4 additions & 0 deletions packages/react-devtools-shared/src/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export function initBackend(
agent.removeListener('shutdown', onAgentShutdown);
});

agent.addListener('updateHookSettings', settings => {
hook.settings = settings;
});

return () => {
subs.forEach(fn => fn());
};
Expand Down
10 changes: 10 additions & 0 deletions packages/react-devtools-shared/src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ export type DevToolsHook = {
// Testing
dangerous_setTargetConsoleForTesting?: (fakeConsole: Object) => void,

settings: DevToolsHookSettings,
settingsHaveBeenInjected: boolean,
injectSettings: (settings: DevToolsHookSettings) => void,
...
};

Expand All @@ -536,3 +539,10 @@ export type ConsolePatchSettings = {
hideConsoleLogsInStrictMode: boolean,
browserTheme: BrowserTheme,
};

export type DevToolsHookSettings = {
appendComponentStack: boolean,
breakOnConsoleErrors: boolean,
showInlineWarningsAndErrors: boolean,
hideConsoleLogsInStrictMode: boolean,
};
27 changes: 26 additions & 1 deletion packages/react-devtools-shared/src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
RendererID,
RendererInterface,
DevToolsBackend,
DevToolsHookSettings,
} from './backend/types';

import {
Expand All @@ -24,7 +25,17 @@ import {

declare var window: any;

export function installHook(target: any): DevToolsHook | null {
const defaultHookSettings: DevToolsHookSettings = {
appendComponentStack: true,
breakOnConsoleErrors: false,
showInlineWarningsAndErrors: true,
hideConsoleLogsInStrictMode: false,
};

export function installHook(
target: any,
settingsToInject?: DevToolsHookSettings,
): DevToolsHook | null {
if (target.hasOwnProperty('__REACT_DEVTOOLS_GLOBAL_HOOK__')) {
return null;
}
Expand Down Expand Up @@ -526,6 +537,16 @@ export function installHook(target: any): DevToolsHook | null {
const renderers = new Map<RendererID, ReactRenderer>();
const backends = new Map<string, DevToolsBackend>();

const settings = settingsToInject || defaultHookSettings;
const settingsHaveBeenInjected = settingsToInject != null;

// Should be used once when the persisted settings were loaded from some asynchronous storage
// This will be used as a signal that Hook initialization has finished, and it can now use patched console implementation
function injectSettings(newSettings: DevToolsHookSettings): void {
hook.settings = newSettings;
hook.settingsHaveBeenInjected = true;
}

const hook: DevToolsHook = {
rendererInterfaces,
listeners,
Expand Down Expand Up @@ -562,6 +583,10 @@ export function installHook(target: any): DevToolsHook | null {
getInternalModuleRanges,
registerInternalModuleStart,
registerInternalModuleStop,

settings,
settingsHaveBeenInjected,
injectSettings,
};

if (__TEST__) {
Expand Down

0 comments on commit 09a7a6a

Please sign in to comment.