Skip to content

Commit dd06b77

Browse files
committed
feat[react-devtools]: add settings to global hook object
1 parent bb6b86e commit dd06b77

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>) => {
@@ -153,6 +151,7 @@ export default class Agent extends EventEmitter<{
153151
drawTraceUpdates: [Array<HostInstance>],
154152
disableTraceUpdates: [],
155153
getIfHasUnsupportedRendererVersion: [],
154+
updateHookSettings: [DevToolsHookSettings],
156155
}> {
157156
_bridge: BackendBridge;
158157
_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
@@ -83,6 +83,10 @@ export function initBackend(
8383
agent.removeListener('shutdown', onAgentShutdown);
8484
});
8585

86+
agent.addListener('updateHookSettings', settings => {
87+
hook.settings = settings;
88+
});
89+
8690
return () => {
8791
subs.forEach(fn => fn());
8892
};

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

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

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

@@ -537,3 +540,10 @@ export type ConsolePatchSettings = {
537540
hideConsoleLogsInStrictMode: boolean,
538541
browserTheme: BrowserTheme,
539542
};
543+
544+
export type DevToolsHookSettings = {
545+
appendComponentStack: boolean,
546+
breakOnConsoleErrors: boolean,
547+
showInlineWarningsAndErrors: boolean,
548+
hideConsoleLogsInStrictMode: boolean,
549+
};

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 {
@@ -25,7 +26,17 @@ import attachRenderer from './attachRenderer';
2526

2627
declare var window: any;
2728

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

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

569594
if (__TEST__) {

0 commit comments

Comments
 (0)