Skip to content

Commit 09a7a6a

Browse files
committed
feat[react-devtools]: add settings to global hook object
1 parent 3dfd5d9 commit 09a7a6a

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

packages/react-devtools-shared/src/backend/agent.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ import type {
3737
RendererID,
3838
RendererInterface,
3939
ConsolePatchSettings,
40+
DevToolsHookSettings,
4041
} from './types';
41-
import type {
42-
ComponentFilter,
43-
BrowserTheme,
44-
} from 'react-devtools-shared/src/frontend/types';
42+
import type {ComponentFilter} from 'react-devtools-shared/src/frontend/types';
4543
import {isSynchronousXHRSupported, isReactNativeEnvironment} from './utils';
4644

4745
const debug = (methodName: string, ...args: Array<string>) => {
@@ -152,6 +150,7 @@ export default class Agent extends EventEmitter<{
152150
traceUpdates: [Set<HostInstance>],
153151
drawTraceUpdates: [Array<HostInstance>],
154152
disableTraceUpdates: [],
153+
updateHookSettings: [DevToolsHookSettings],
155154
}> {
156155
_bridge: BackendBridge;
157156
_isProfiling: boolean = false;
@@ -805,30 +804,22 @@ export default class Agent extends EventEmitter<{
805804
}
806805
};
807806

808-
updateConsolePatchSettings: ({
809-
appendComponentStack: boolean,
810-
breakOnConsoleErrors: boolean,
811-
browserTheme: BrowserTheme,
812-
hideConsoleLogsInStrictMode: boolean,
813-
showInlineWarningsAndErrors: boolean,
814-
}) => void = ({
815-
appendComponentStack,
816-
breakOnConsoleErrors,
817-
showInlineWarningsAndErrors,
818-
hideConsoleLogsInStrictMode,
819-
browserTheme,
820-
}: ConsolePatchSettings) => {
807+
updateConsolePatchSettings: (
808+
settings: $ReadOnly<ConsolePatchSettings>,
809+
) => void = settings => {
810+
// Propagate the settings, so Backend can subscribe to it and modify hook
811+
this.emit('updateHookSettings', {
812+
appendComponentStack: settings.appendComponentStack,
813+
breakOnConsoleErrors: settings.breakOnConsoleErrors,
814+
showInlineWarningsAndErrors: settings.showInlineWarningsAndErrors,
815+
hideConsoleLogsInStrictMode: settings.hideConsoleLogsInStrictMode,
816+
});
817+
821818
// If the frontend preferences have changed,
822819
// or in the case of React Native- if the backend is just finding out the preferences-
823820
// then reinstall the console overrides.
824821
// It's safe to call `patchConsole` multiple times.
825-
patchConsole({
826-
appendComponentStack,
827-
breakOnConsoleErrors,
828-
showInlineWarningsAndErrors,
829-
hideConsoleLogsInStrictMode,
830-
browserTheme,
831-
});
822+
patchConsole(settings);
832823
};
833824

834825
updateComponentFilters: (componentFilters: Array<ComponentFilter>) => void =

packages/react-devtools-shared/src/backend/console.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function patch({
135135
showInlineWarningsAndErrors,
136136
hideConsoleLogsInStrictMode,
137137
browserTheme,
138-
}: ConsolePatchSettings): void {
138+
}: $ReadOnly<ConsolePatchSettings>): void {
139139
// Settings may change after we've patched the console.
140140
// Using a shared ref allows the patch function to read the latest values.
141141
consoleSettingsRef.appendComponentStack = appendComponentStack;

packages/react-devtools-shared/src/backend/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ export function initBackend(
140140
agent.removeListener('shutdown', onAgentShutdown);
141141
});
142142

143+
agent.addListener('updateHookSettings', settings => {
144+
hook.settings = settings;
145+
});
146+
143147
return () => {
144148
subs.forEach(fn => fn());
145149
};

packages/react-devtools-shared/src/backend/types.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ export type DevToolsHook = {
526526
// Testing
527527
dangerous_setTargetConsoleForTesting?: (fakeConsole: Object) => void,
528528

529+
settings: DevToolsHookSettings,
530+
settingsHaveBeenInjected: boolean,
531+
injectSettings: (settings: DevToolsHookSettings) => void,
529532
...
530533
};
531534

@@ -536,3 +539,10 @@ export type ConsolePatchSettings = {
536539
hideConsoleLogsInStrictMode: boolean,
537540
browserTheme: BrowserTheme,
538541
};
542+
543+
export type DevToolsHookSettings = {
544+
appendComponentStack: boolean,
545+
breakOnConsoleErrors: boolean,
546+
showInlineWarningsAndErrors: boolean,
547+
hideConsoleLogsInStrictMode: boolean,
548+
};

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
RendererID,
1616
RendererInterface,
1717
DevToolsBackend,
18+
DevToolsHookSettings,
1819
} from './backend/types';
1920

2021
import {
@@ -24,7 +25,17 @@ import {
2425

2526
declare var window: any;
2627

27-
export function installHook(target: any): DevToolsHook | null {
28+
const defaultHookSettings: DevToolsHookSettings = {
29+
appendComponentStack: true,
30+
breakOnConsoleErrors: false,
31+
showInlineWarningsAndErrors: true,
32+
hideConsoleLogsInStrictMode: false,
33+
};
34+
35+
export function installHook(
36+
target: any,
37+
settingsToInject?: DevToolsHookSettings,
38+
): DevToolsHook | null {
2839
if (target.hasOwnProperty('__REACT_DEVTOOLS_GLOBAL_HOOK__')) {
2940
return null;
3041
}
@@ -526,6 +537,16 @@ export function installHook(target: any): DevToolsHook | null {
526537
const renderers = new Map<RendererID, ReactRenderer>();
527538
const backends = new Map<string, DevToolsBackend>();
528539

540+
const settings = settingsToInject || defaultHookSettings;
541+
const settingsHaveBeenInjected = settingsToInject != null;
542+
543+
// Should be used once when the persisted settings were loaded from some asynchronous storage
544+
// This will be used as a signal that Hook initialization has finished, and it can now use patched console implementation
545+
function injectSettings(newSettings: DevToolsHookSettings): void {
546+
hook.settings = newSettings;
547+
hook.settingsHaveBeenInjected = true;
548+
}
549+
529550
const hook: DevToolsHook = {
530551
rendererInterfaces,
531552
listeners,
@@ -562,6 +583,10 @@ export function installHook(target: any): DevToolsHook | null {
562583
getInternalModuleRanges,
563584
registerInternalModuleStart,
564585
registerInternalModuleStop,
586+
587+
settings,
588+
settingsHaveBeenInjected,
589+
injectSettings,
565590
};
566591

567592
if (__TEST__) {

0 commit comments

Comments
 (0)