From 6cf0247192f9e1fd6380ce5fc792f321686ffc4f Mon Sep 17 00:00:00 2001 From: Robert Balicki Date: Thu, 8 Sep 2022 12:52:57 -0400 Subject: [PATCH] [react devtools] device storage --- .../src/backend/agent.js | 28 +++++- .../src/backend/deviceStorage/index.js | 95 +++++++++++++++++++ .../src/backend/deviceStorage/initialize.js | 58 +++++++++++ .../src/backend/renderer.js | 23 +---- .../src/backend/types.js | 7 ++ .../views/Settings/SettingsContext.js | 47 +++++++++ packages/react-devtools-shared/src/hook.js | 35 ++----- 7 files changed, 244 insertions(+), 49 deletions(-) create mode 100644 packages/react-devtools-shared/src/backend/deviceStorage/index.js create mode 100644 packages/react-devtools-shared/src/backend/deviceStorage/initialize.js diff --git a/packages/react-devtools-shared/src/backend/agent.js b/packages/react-devtools-shared/src/backend/agent.js index 222a883ce7619..bd61c35962b3b 100644 --- a/packages/react-devtools-shared/src/backend/agent.js +++ b/packages/react-devtools-shared/src/backend/agent.js @@ -13,6 +13,11 @@ import { SESSION_STORAGE_LAST_SELECTION_KEY, SESSION_STORAGE_RELOAD_AND_PROFILE_KEY, SESSION_STORAGE_RECORD_CHANGE_DESCRIPTIONS_KEY, + LOCAL_STORAGE_BROWSER_THEME, + LOCAL_STORAGE_SHOULD_BREAK_ON_CONSOLE_ERRORS, + LOCAL_STORAGE_SHOULD_APPEND_COMPONENT_STACK_KEY, + LOCAL_STORAGE_SHOW_INLINE_WARNINGS_AND_ERRORS_KEY, + LOCAL_STORAGE_HIDE_CONSOLE_LOGS_IN_STRICT_MODE, __DEBUG__, } from '../constants'; import { @@ -20,6 +25,7 @@ import { sessionStorageRemoveItem, sessionStorageSetItem, } from 'react-devtools-shared/src/storage'; +import {storeSettingInDeviceStorage} from 'react-devtools-shared/src/backend/deviceStorage'; import setupHighlighter from './views/Highlighter'; import { initialize as setupTraceUpdates, @@ -683,7 +689,27 @@ export default class Agent extends EventEmitter<{| hideConsoleLogsInStrictMode: boolean, browserTheme: BrowserTheme, |}) => { - // If the frontend preference has change, + // Store the current settings in the device storage cache so that, if the app + // is restarted, we can access those settings before the DevTools frontend connects + storeSettingInDeviceStorage( + LOCAL_STORAGE_SHOULD_APPEND_COMPONENT_STACK_KEY, + JSON.stringify(appendComponentStack), + ); + storeSettingInDeviceStorage( + LOCAL_STORAGE_SHOULD_BREAK_ON_CONSOLE_ERRORS, + JSON.stringify(breakOnConsoleErrors), + ); + storeSettingInDeviceStorage( + LOCAL_STORAGE_SHOW_INLINE_WARNINGS_AND_ERRORS_KEY, + JSON.stringify(showInlineWarningsAndErrors), + ); + storeSettingInDeviceStorage( + LOCAL_STORAGE_HIDE_CONSOLE_LOGS_IN_STRICT_MODE, + JSON.stringify(hideConsoleLogsInStrictMode), + ); + storeSettingInDeviceStorage(LOCAL_STORAGE_BROWSER_THEME, browserTheme); + + // If the frontend preferences have changed, // or in the case of React Native- if the backend is just finding out the preference- // then reinstall the console overrides. // It's safe to call these methods multiple times, so we don't need to worry about that. diff --git a/packages/react-devtools-shared/src/backend/deviceStorage/index.js b/packages/react-devtools-shared/src/backend/deviceStorage/index.js new file mode 100644 index 0000000000000..ab8c54b7ec6de --- /dev/null +++ b/packages/react-devtools-shared/src/backend/deviceStorage/index.js @@ -0,0 +1,95 @@ +/** + * Copyright (c) Facebook, 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 {InjectedDeviceStorageMethods} from 'react-devtools-shared/src/backend/types'; + +export {initializeFromDeviceStorage} from './initialize'; +import {initializeFromDeviceStorage} from './initialize'; + +// # How does React Native/React access settings during startup, potentially +// before the DevTools frontend has connected? +// +// During startup, +// - React Native reads those settings from device storage and writes them to +// the window object. +// - React does need to do this, as the DevTools extension (if installed) can +// execute code that runs before the site JS. This code reads these settings +// from the localStorage of the extension frontend and adds a