From f2c57a31e9953b3889c56f68e129e67afca15d0e Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Wed, 18 Sep 2024 18:30:32 +0100 Subject: [PATCH] chore: remove settings manager from react-devtools-core (#30986) Stacked on https://github.com/facebook/react/pull/30636. See [this commit](https://github.com/facebook/react/pull/30986/commits/20cec76c44f77e74b3a85225fecab5a431cd986f). This has been only used for React Native and will be replaced by another approach (initialization via `installHook` call) in the next PR. --- packages/react-devtools-core/src/backend.js | 44 ----------- .../react-devtools-core/src/cachedSettings.js | 75 ------------------- .../src/backend/console.js | 26 ------- .../src/backend/types.js | 3 - 4 files changed, 148 deletions(-) delete mode 100644 packages/react-devtools-core/src/cachedSettings.js delete mode 100644 packages/react-devtools-shared/src/backend/console.js diff --git a/packages/react-devtools-core/src/backend.js b/packages/react-devtools-core/src/backend.js index 6949d6019145a..84f537e875d00 100644 --- a/packages/react-devtools-core/src/backend.js +++ b/packages/react-devtools-core/src/backend.js @@ -14,11 +14,6 @@ import {initBackend} from 'react-devtools-shared/src/backend'; import {__DEBUG__} from 'react-devtools-shared/src/constants'; import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor'; import {getDefaultComponentFilters} from 'react-devtools-shared/src/utils'; -import { - initializeUsingCachedSettings, - cacheConsolePatchSettings, - type DevToolsSettingsManager, -} from './cachedSettings'; import type {BackendBridge} from 'react-devtools-shared/src/bridge'; import type { @@ -37,7 +32,6 @@ type ConnectOptions = { retryConnectionDelay?: number, isAppActive?: () => boolean, websocket?: ?WebSocket, - devToolsSettingsManager: ?DevToolsSettingsManager, }; installHook(window); @@ -72,7 +66,6 @@ export function connectToDevTools(options: ?ConnectOptions) { resolveRNStyle = (null: $FlowFixMe), retryConnectionDelay = 2000, isAppActive = () => true, - devToolsSettingsManager, } = options || {}; const protocol = useHttps ? 'wss' : 'ws'; @@ -88,16 +81,6 @@ export function connectToDevTools(options: ?ConnectOptions) { } } - if (devToolsSettingsManager != null) { - try { - initializeUsingCachedSettings(devToolsSettingsManager); - } catch (e) { - // If we call a method on devToolsSettingsManager that throws, or if - // is invalid data read out, don't throw and don't interrupt initialization - console.error(e); - } - } - if (!isAppActive()) { // If the app is in background, maybe retry later. // Don't actually attempt to connect until we're in foreground. @@ -161,15 +144,6 @@ export function connectToDevTools(options: ?ConnectOptions) { }, ); - if (devToolsSettingsManager != null && bridge != null) { - bridge.addListener('updateHookSettings', consolePatchSettings => - cacheConsolePatchSettings( - devToolsSettingsManager, - consolePatchSettings, - ), - ); - } - // The renderer interface doesn't read saved component filters directly, // because they are generally stored in localStorage within the context of the extension. // Because of this it relies on the extension to pass filters. @@ -314,7 +288,6 @@ type ConnectWithCustomMessagingOptions = { onSubscribe: (cb: Function) => void, onUnsubscribe: (cb: Function) => void, onMessage: (event: string, payload: any) => void, - settingsManager: ?DevToolsSettingsManager, nativeStyleEditorValidAttributes?: $ReadOnlyArray, resolveRNStyle?: ResolveNativeStyle, }; @@ -323,7 +296,6 @@ export function connectWithCustomMessagingProtocol({ onSubscribe, onUnsubscribe, onMessage, - settingsManager, nativeStyleEditorValidAttributes, resolveRNStyle, }: ConnectWithCustomMessagingOptions): Function { @@ -332,16 +304,6 @@ export function connectWithCustomMessagingProtocol({ return; } - if (settingsManager != null) { - try { - initializeUsingCachedSettings(settingsManager); - } catch (e) { - // If we call a method on devToolsSettingsManager that throws, or if - // is invalid data read out, don't throw and don't interrupt initialization - console.error(e); - } - } - const wall: Wall = { listen(fn: Function) { onSubscribe(fn); @@ -367,12 +329,6 @@ export function connectWithCustomMessagingProtocol({ }, ); - if (settingsManager != null) { - bridge.addListener('updateHookSettings', consolePatchSettings => - cacheConsolePatchSettings(settingsManager, consolePatchSettings), - ); - } - if (window.__REACT_DEVTOOLS_COMPONENT_FILTERS__ == null) { bridge.send('overrideComponentFilters', savedComponentFilters); } diff --git a/packages/react-devtools-core/src/cachedSettings.js b/packages/react-devtools-core/src/cachedSettings.js deleted file mode 100644 index 5c603447e8d88..0000000000000 --- a/packages/react-devtools-core/src/cachedSettings.js +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -import type {ConsolePatchSettings} from 'react-devtools-shared/src/backend/types'; -import {writeConsolePatchSettingsToWindow} from 'react-devtools-shared/src/backend/console'; -import {castBool} from 'react-devtools-shared/src/utils'; - -// Note: all keys should be optional in this type, because users can use newer -// versions of React DevTools with older versions of React Native, and the object -// provided by React Native may not include all of this type's fields. -export type DevToolsSettingsManager = { - getConsolePatchSettings: ?() => string, - setConsolePatchSettings: ?(key: string) => void, -}; - -export function initializeUsingCachedSettings( - devToolsSettingsManager: DevToolsSettingsManager, -) { - initializeConsolePatchSettings(devToolsSettingsManager); -} - -function initializeConsolePatchSettings( - devToolsSettingsManager: DevToolsSettingsManager, -) { - if (devToolsSettingsManager.getConsolePatchSettings == null) { - return; - } - const consolePatchSettingsString = - devToolsSettingsManager.getConsolePatchSettings(); - if (consolePatchSettingsString == null) { - return; - } - const parsedConsolePatchSettings = parseConsolePatchSettings( - consolePatchSettingsString, - ); - if (parsedConsolePatchSettings == null) { - return; - } - writeConsolePatchSettingsToWindow(parsedConsolePatchSettings); -} - -function parseConsolePatchSettings( - consolePatchSettingsString: string, -): ?ConsolePatchSettings { - const parsedValue = JSON.parse(consolePatchSettingsString ?? '{}'); - const { - appendComponentStack, - breakOnConsoleErrors, - showInlineWarningsAndErrors, - hideConsoleLogsInStrictMode, - } = parsedValue; - - return { - appendComponentStack: castBool(appendComponentStack) ?? true, - breakOnConsoleErrors: castBool(breakOnConsoleErrors) ?? false, - showInlineWarningsAndErrors: castBool(showInlineWarningsAndErrors) ?? true, - hideConsoleLogsInStrictMode: castBool(hideConsoleLogsInStrictMode) ?? false, - }; -} - -export function cacheConsolePatchSettings( - devToolsSettingsManager: DevToolsSettingsManager, - value: $ReadOnly, -): void { - if (devToolsSettingsManager.setConsolePatchSettings == null) { - return; - } - devToolsSettingsManager.setConsolePatchSettings(JSON.stringify(value)); -} diff --git a/packages/react-devtools-shared/src/backend/console.js b/packages/react-devtools-shared/src/backend/console.js deleted file mode 100644 index 9e61285b50fe4..0000000000000 --- a/packages/react-devtools-shared/src/backend/console.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -import type {ConsolePatchSettings} from './types'; - -// After receiving cached console patch settings from React Native, we set them on window. -// When the console is initially patched (in renderer.js and hook.js), these values are read. -// The browser extension (etc.) sets these values on window, but through another method. -export function writeConsolePatchSettingsToWindow( - settings: ConsolePatchSettings, -): void { - window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = - settings.appendComponentStack; - window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = - settings.breakOnConsoleErrors; - window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ = - settings.showInlineWarningsAndErrors; - window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = - settings.hideConsoleLogsInStrictMode; -} diff --git a/packages/react-devtools-shared/src/backend/types.js b/packages/react-devtools-shared/src/backend/types.js index 4065b536afeeb..c3110dc517fdc 100644 --- a/packages/react-devtools-shared/src/backend/types.js +++ b/packages/react-devtools-shared/src/backend/types.js @@ -534,6 +534,3 @@ export type DevToolsHookSettings = { showInlineWarningsAndErrors: boolean, hideConsoleLogsInStrictMode: boolean, }; - -// Will be removed together with console patching from backend/console.js to hook.js -export type ConsolePatchSettings = DevToolsHookSettings;