Skip to content

Commit 24e529b

Browse files
committed
feat[react-devtools]: add settings to global hook object
1 parent 2b00018 commit 24e529b

5 files changed

Lines changed: 43 additions & 26 deletions

File tree

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

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

4846
const debug = (methodName: string, ...args: Array<string>) => {
@@ -153,6 +151,7 @@ export default class Agent extends EventEmitter<{
153151
traceUpdates: [Set<HostInstance>],
154152
drawTraceUpdates: [Array<HostInstance>],
155153
disableTraceUpdates: [],
154+
updateHookSettings: [DevToolsHookSettings],
156155
}> {
157156
_bridge: BackendBridge;
158157
_isProfiling: boolean = false;
@@ -762,30 +761,22 @@ export default class Agent extends EventEmitter<{
762761
}
763762
};
764763

765-
updateConsolePatchSettings: ({
766-
appendComponentStack: boolean,
767-
breakOnConsoleErrors: boolean,
768-
browserTheme: BrowserTheme,
769-
hideConsoleLogsInStrictMode: boolean,
770-
showInlineWarningsAndErrors: boolean,
771-
}) => void = ({
772-
appendComponentStack,
773-
breakOnConsoleErrors,
774-
showInlineWarningsAndErrors,
775-
hideConsoleLogsInStrictMode,
776-
browserTheme,
777-
}: ConsolePatchSettings) => {
764+
updateConsolePatchSettings: (
765+
settings: $ReadOnly<ConsolePatchSettings>,
766+
) => void = settings => {
767+
// Propagate the settings, so Backend can subscribe to it and modify hook
768+
this.emit('updateHookSettings', {
769+
appendComponentStack: settings.appendComponentStack,
770+
breakOnConsoleErrors: settings.breakOnConsoleErrors,
771+
showInlineWarningsAndErrors: settings.showInlineWarningsAndErrors,
772+
hideConsoleLogsInStrictMode: settings.hideConsoleLogsInStrictMode,
773+
});
774+
778775
// If the frontend preferences have changed,
779776
// or in the case of React Native- if the backend is just finding out the preferences-
780777
// then reinstall the console overrides.
781778
// It's safe to call `patchConsole` multiple times.
782-
patchConsole({
783-
appendComponentStack,
784-
breakOnConsoleErrors,
785-
showInlineWarningsAndErrors,
786-
hideConsoleLogsInStrictMode,
787-
browserTheme,
788-
});
779+
patchConsole(settings);
789780
};
790781

791782
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
@@ -177,7 +177,7 @@ export function patch({
177177
showInlineWarningsAndErrors,
178178
hideConsoleLogsInStrictMode,
179179
browserTheme,
180-
}: ConsolePatchSettings): void {
180+
}: $ReadOnly<ConsolePatchSettings>): void {
181181
// Settings may change after we've patched the console.
182182
// Using a shared ref allows the patch function to read the latest values.
183183
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
@@ -130,6 +130,10 @@ export function initBackend(
130130
agent.removeListener('shutdown', onAgentShutdown);
131131
});
132132

133+
agent.addListener('updateHookSettings', settings => {
134+
hook.settings = settings;
135+
});
136+
133137
return () => {
134138
subs.forEach(fn => fn());
135139
};

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ export type DevToolsHook = {
513513
// Testing
514514
dangerous_setTargetConsoleForTesting?: (fakeConsole: Object) => void,
515515

516+
settings: DevToolsHookSettings,
516517
...
517518
};
518519

@@ -523,3 +524,10 @@ export type ConsolePatchSettings = {
523524
hideConsoleLogsInStrictMode: boolean,
524525
browserTheme: BrowserTheme,
525526
};
527+
528+
export type DevToolsHookSettings = {
529+
appendComponentStack: boolean,
530+
breakOnConsoleErrors: boolean,
531+
showInlineWarningsAndErrors: boolean,
532+
hideConsoleLogsInStrictMode: boolean,
533+
};

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

Lines changed: 15 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,10 @@ import {
2425

2526
declare var window: any;
2627

27-
export function installHook(target: any): DevToolsHook | null {
28+
export function installHook(
29+
target: any,
30+
injectedSettings?: DevToolsHookSettings,
31+
): DevToolsHook | null {
2832
if (target.hasOwnProperty('__REACT_DEVTOOLS_GLOBAL_HOOK__')) {
2933
return null;
3034
}
@@ -543,6 +547,14 @@ export function installHook(target: any): DevToolsHook | null {
543547
const listeners: {[string]: Array<Handler>} = {};
544548
const renderers = new Map<RendererID, ReactRenderer>();
545549
const backends = new Map<string, DevToolsBackend>();
550+
const settings = injectedSettings
551+
? injectedSettings
552+
: {
553+
appendComponentStack: true,
554+
breakOnConsoleErrors: false,
555+
showInlineWarningsAndErrors: true,
556+
hideConsoleLogsInStrictMode: false,
557+
};
546558

547559
const hook: DevToolsHook = {
548560
rendererInterfaces,
@@ -577,6 +589,8 @@ export function installHook(target: any): DevToolsHook | null {
577589
getInternalModuleRanges,
578590
registerInternalModuleStart,
579591
registerInternalModuleStop,
592+
593+
settings,
580594
};
581595

582596
if (__TEST__) {

0 commit comments

Comments
 (0)