Skip to content

Commit

Permalink
refactor[react-devtools]: propagate settings from global hook object …
Browse files Browse the repository at this point in the history
…to frontend
  • Loading branch information
hoxyq committed Aug 6, 2024
1 parent 0c7752c commit 4a4600e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 25 deletions.
13 changes: 13 additions & 0 deletions packages/react-devtools-shared/src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export default class Agent extends EventEmitter<{
drawTraceUpdates: [Array<HostInstance>],
disableTraceUpdates: [],
updateHookSettings: [DevToolsHookSettings],
fetchHookSettings: [],
}> {
_bridge: BackendBridge;
_isProfiling: boolean = false;
Expand Down Expand Up @@ -214,10 +215,13 @@ export default class Agent extends EventEmitter<{
this.syncSelectionFromBuiltinElementsPanel,
);
bridge.addListener('shutdown', this.shutdown);

bridge.addListener(
'updateConsolePatchSettings',
this.updateConsolePatchSettings,
);
bridge.addListener('fetchHookSettings', this.fetchHookSettings);

bridge.addListener('updateComponentFilters', this.updateComponentFilters);
bridge.addListener('viewAttributeSource', this.viewAttributeSource);
bridge.addListener('viewElementSource', this.viewElementSource);
Expand Down Expand Up @@ -772,6 +776,15 @@ export default class Agent extends EventEmitter<{
});
};

fetchHookSettings: () => void = () => {
this.emit('fetchHookSettings');
};

onHookSettings: (settings: $ReadOnly<ConsolePatchSettings>) => void =
settings => {
this._bridge.send('hookSettings', settings);
};

updateComponentFilters: (componentFilters: Array<ComponentFilter>) => void =
componentFilters => {
for (const rendererID in this._rendererInterfaces) {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-devtools-shared/src/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export function initBackend(
agent.addListener('updateHookSettings', settings => {
hook.settings = settings;
});
agent.addListener('fetchHookSettings', () => {
agent.onHookSettings(hook.settings);
});

return () => {
subs.forEach(fn => fn());
Expand Down
5 changes: 5 additions & 0 deletions packages/react-devtools-shared/src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
ProfilingDataBackend,
RendererID,
ConsolePatchSettings,
DevToolsHookSettings,
} from 'react-devtools-shared/src/backend/types';
import type {StyleAndLayout as StyleAndLayoutPayload} from 'react-devtools-shared/src/backend/NativeStyleEditor/types';

Expand Down Expand Up @@ -207,6 +208,8 @@ export type BackendEvents = {
{isSupported: boolean, validAttributes: ?$ReadOnlyArray<string>},
],
NativeStyleEditor_styleAndLayout: [StyleAndLayoutPayload],

hookSettings: [$ReadOnly<DevToolsHookSettings>],
};

type FrontendEvents = {
Expand Down Expand Up @@ -265,6 +268,8 @@ type FrontendEvents = {

resumeElementPolling: [],
pauseElementPolling: [],

fetchHookSettings: [],
};

class Bridge<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ import {
import {
LOCAL_STORAGE_BROWSER_THEME,
LOCAL_STORAGE_PARSE_HOOK_NAMES_KEY,
LOCAL_STORAGE_SHOULD_BREAK_ON_CONSOLE_ERRORS,
LOCAL_STORAGE_SHOULD_APPEND_COMPONENT_STACK_KEY,
LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY,
LOCAL_STORAGE_SHOW_INLINE_WARNINGS_AND_ERRORS_KEY,
LOCAL_STORAGE_HIDE_CONSOLE_LOGS_IN_STRICT_MODE,
} from 'react-devtools-shared/src/constants';
import {
COMFORTABLE_LINE_HEIGHT,
COMPACT_LINE_HEIGHT,
} from 'react-devtools-shared/src/devtools/constants';
import {useLocalStorage} from '../hooks';
import {useDevToolsHookSettings, useLocalStorage} from '../hooks';
import {BridgeContext} from '../context';
import {logEvent} from 'react-devtools-shared/src/Logger';

Expand Down Expand Up @@ -118,36 +114,27 @@ function SettingsContextController({
LOCAL_STORAGE_BROWSER_THEME,
'auto',
);
const [appendComponentStack, setAppendComponentStack] =
useLocalStorageWithLog<boolean>(
LOCAL_STORAGE_SHOULD_APPEND_COMPONENT_STACK_KEY,
true,
);
const [breakOnConsoleErrors, setBreakOnConsoleErrors] =
useLocalStorageWithLog<boolean>(
LOCAL_STORAGE_SHOULD_BREAK_ON_CONSOLE_ERRORS,
false,
);
const [parseHookNames, setParseHookNames] = useLocalStorageWithLog<boolean>(
LOCAL_STORAGE_PARSE_HOOK_NAMES_KEY,
false,
);
const [hideConsoleLogsInStrictMode, setHideConsoleLogsInStrictMode] =
useLocalStorageWithLog<boolean>(
LOCAL_STORAGE_HIDE_CONSOLE_LOGS_IN_STRICT_MODE,
false,
);
const [showInlineWarningsAndErrors, setShowInlineWarningsAndErrors] =
useLocalStorageWithLog<boolean>(
LOCAL_STORAGE_SHOW_INLINE_WARNINGS_AND_ERRORS_KEY,
true,
);
const [traceUpdatesEnabled, setTraceUpdatesEnabled] =
useLocalStorageWithLog<boolean>(
LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY,
false,
);

const {
appendComponentStack,
setAppendComponentStack,
breakOnConsoleErrors,
setBreakOnConsoleErrors,
hideConsoleLogsInStrictMode,
setHideConsoleLogsInStrictMode,
showInlineWarningsAndErrors,
setShowInlineWarningsAndErrors,
} = useDevToolsHookSettings(bridge);

const documentElements = useMemo<DocumentElements>(() => {
const array: Array<HTMLElement> = [
((document.documentElement: any): HTMLElement),
Expand Down
53 changes: 53 additions & 0 deletions packages/react-devtools-shared/src/devtools/views/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from 'react-devtools-shared/src/storage';
import {StoreContext, BridgeContext} from './context';
import {sanitizeForParse, smartParse, smartStringify} from '../utils';
import type {FrontendBridge} from '../../bridge';
import type {DevToolsHookSettings} from 'react-devtools-shared/src/backend/types';

type ACTION_RESET = {
type: 'RESET',
Expand Down Expand Up @@ -209,6 +211,57 @@ export function useLocalStorage<T>(
return [storedValue, setValue];
}

type DevToolsHookSettingsGettersAndSetters = {
appendComponentStack: boolean,
setAppendComponentStack: boolean => void,
breakOnConsoleErrors: boolean,
setBreakOnConsoleErrors: boolean => void,
hideConsoleLogsInStrictMode: boolean,
setHideConsoleLogsInStrictMode: boolean => void,
showInlineWarningsAndErrors: boolean,
setShowInlineWarningsAndErrors: boolean => void,
};

export function useDevToolsHookSettings(
bridge: FrontendBridge,
): DevToolsHookSettingsGettersAndSetters {
const [appendComponentStack, setAppendComponentStack] = useState(true);
const [breakOnConsoleErrors, setBreakOnConsoleErrors] = useState(false);
const [hideConsoleLogsInStrictMode, setHideConsoleLogsInStrictMode] =
useState(false);
const [showInlineWarningsAndErrors, setShowInlineWarningsAndErrors] =
useState(false);

const onHookSettings = useCallback(
(settings: $ReadOnly<DevToolsHookSettings>) => {
setAppendComponentStack(settings.appendComponentStack);
setBreakOnConsoleErrors(settings.breakOnConsoleErrors);
setHideConsoleLogsInStrictMode(settings.hideConsoleLogsInStrictMode);
setShowInlineWarningsAndErrors(settings.showInlineWarningsAndErrors);
},
[],
);
useEffect(() => {
bridge.addListener('hookSettings', onHookSettings);
bridge.send('fetchHookSettings');

return () => {
bridge.removeListener('hookSettings', onHookSettings);
};
}, [bridge]);

return {
appendComponentStack,
setAppendComponentStack,
breakOnConsoleErrors,
setBreakOnConsoleErrors,
hideConsoleLogsInStrictMode,
setHideConsoleLogsInStrictMode,
showInlineWarningsAndErrors,
setShowInlineWarningsAndErrors,
};
}

export function useModalDismissSignal(
modalRef: {current: HTMLDivElement | null, ...},
dismissCallback: () => void,
Expand Down

0 comments on commit 4a4600e

Please sign in to comment.