@samitouri / QOS-React / commits / fecc288b7d

[react devtools] Device storage support (#25452)

# Summary * This PR adds support for persisting certain settings to device storage, allowing e.g. RN apps to properly patch the console when restarted. * The device storage APIs have signature `getConsolePatchSettings()` and `setConsolePatchSettings(string)`, in iOS, are thin wrappers around the `Library/Settings` turbomodule, and wrap a new TM that uses the `SharedPreferences` class in Android. * Pass device storage getters/setters from RN to DevTools' `connectToDevtools`. The setters are then used to populate values on `window`. Later, the console is patched using these values. * If we receive a notification from DevTools that the console patching fields have been updated, we write values back to local storage. * See https://github.com/facebook/react-native/pull/34903 # How did you test this change? Manual testing, `yarn run test-build-devtools`, `yarn run prettier`, `yarn run flow dom` ## Manual testing setup: ### React DevTools Frontend * Get the DevTools frontend in flipper: * `nvm install -g react-devtools-core`, then replace that package with a symlink to the local package * enable "use globally installed devtools" in flipper * yarn run start in react-devtools, etc. as well ### React DevTools Backend * `yarn run build:backend` in react-devtools-core, then copy-paste that file to the expo app's node_modules directory ### React Native * A local version of React Native can be patched in by modifying an expo app's package.json, as in `"react-native": "rbalicki2/react-native#branch-name"` # Versioning safety * There are three versioned modules to worry about: react native, the devtools frontend and the devtools backend. * The react devtools backend checks for whether a `cachedSettingsStore` is passed from react native. If not (e.g. if React Native is outdated), then no behavior changes. * The devtools backend reads the patched console values from the cached settings store. However, if nothing has been stored, for example because the frontend is outdated or has never synced its settings, then behavior doesn't change. * The devtools frontend sends no new messages. However, if it did send a new message (e.g. "store this value at this key"), and the backend was outdated, that message would be silently ignored.

Robert Balicki committed Oct 25, 2022 at 13:25 UTC fecc288b7dce182fbc3056b3f98a445a95fb1e68
6 files changed +148 -32
packages/react-devtools-core/src/backend.js
+26
@@ -14,6 +14,11 @@ import {initBackend} from 'react-devtools-shared/src/backend';
14 import {__DEBUG__} from 'react-devtools-shared/src/constants';
15 import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor';
16 import {getDefaultComponentFilters} from 'react-devtools-shared/src/utils';
17 +import {
18 + initializeUsingCachedSettings,
19 + cacheConsolePatchSettings,
20 + type DevToolsSettingsManager,
21 +} from './cachedSettings';
22
23 import type {BackendBridge} from 'react-devtools-shared/src/bridge';
24 import type {ComponentFilter} from 'react-devtools-shared/src/types';
@@ -29,6 +34,7 @@ type ConnectOptions = {
34 retryConnectionDelay?: number,
35 isAppActive?: () => boolean,
36 websocket?: ?WebSocket,
37 + devToolsSettingsManager: ?DevToolsSettingsManager,
38 ...
39 };
40
@@ -63,6 +69,7 @@ export function connectToDevTools(options: ?ConnectOptions) {
69 resolveRNStyle = null,
70 retryConnectionDelay = 2000,
71 isAppActive = () => true,
72 + devToolsSettingsManager,
73 } = options || {};
74
75 const protocol = useHttps ? 'wss' : 'ws';
@@ -78,6 +85,16 @@ export function connectToDevTools(options: ?ConnectOptions) {
85 }
86 }
87
88 + if (devToolsSettingsManager != null) {
89 + try {
90 + initializeUsingCachedSettings(devToolsSettingsManager);
91 + } catch (e) {
92 + // If we call a method on devToolsSettingsManager that throws, or if
93 + // is invalid data read out, don't throw and don't interrupt initialization
94 + console.error(e);
95 + }
96 + }
97 +
98 if (!isAppActive()) {
99 // If the app is in background, maybe retry later.
100 // Don't actually attempt to connect until we're in foreground.
@@ -142,6 +159,15 @@ export function connectToDevTools(options: ?ConnectOptions) {
159 },
160 );
161
162 + if (devToolsSettingsManager != null && bridge != null) {
163 + bridge.addListener('updateConsolePatchSettings', consolePatchSettings =>
164 + cacheConsolePatchSettings(
165 + devToolsSettingsManager,
166 + consolePatchSettings,
167 + ),
168 + );
169 + }
170 +
171 // The renderer interface doesn't read saved component filters directly,
172 // because they are generally stored in localStorage within the context of the extension.
173 // Because of this it relies on the extension to pass filters.
packages/react-devtools-core/src/cachedSettings.js new
+77
@@ -0,0 +1,77 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + *
7 + * @flow
8 + */
9 +
10 +import {
11 + type ConsolePatchSettings,
12 + writeConsolePatchSettingsToWindow,
13 +} from 'react-devtools-shared/src/backend/console';
14 +import {castBool, castBrowserTheme} from 'react-devtools-shared/src/utils';
15 +
16 +// Note: all keys should be optional in this type, because users can use newer
17 +// versions of React DevTools with older versions of React Native, and the object
18 +// provided by React Native may not include all of this type's fields.
19 +export type DevToolsSettingsManager = {
20 + getConsolePatchSettings: ?() => string,
21 + setConsolePatchSettings: ?(key: string) => void,
22 +};
23 +
24 +export function initializeUsingCachedSettings(
25 + devToolsSettingsManager: DevToolsSettingsManager,
26 +) {
27 + initializeConsolePatchSettings(devToolsSettingsManager);
28 +}
29 +
30 +function initializeConsolePatchSettings(
31 + devToolsSettingsManager: DevToolsSettingsManager,
32 +) {
33 + if (devToolsSettingsManager.getConsolePatchSettings == null) {
34 + return;
35 + }
36 + const consolePatchSettingsString = devToolsSettingsManager.getConsolePatchSettings();
37 + if (consolePatchSettingsString == null) {
38 + return;
39 + }
40 + const parsedConsolePatchSettings = parseConsolePatchSettings(
41 + consolePatchSettingsString,
42 + );
43 + if (parsedConsolePatchSettings == null) {
44 + return;
45 + }
46 + writeConsolePatchSettingsToWindow(parsedConsolePatchSettings);
47 +}
48 +
49 +function parseConsolePatchSettings(
50 + consolePatchSettingsString: string,
51 +): ?ConsolePatchSettings {
52 + const parsedValue = JSON.parse(consolePatchSettingsString ?? '{}');
53 + const {
54 + appendComponentStack,
55 + breakOnConsoleErrors,
56 + showInlineWarningsAndErrors,
57 + hideConsoleLogsInStrictMode,
58 + browserTheme,
59 + } = parsedValue;
60 + return {
61 + appendComponentStack: castBool(appendComponentStack) ?? true,
62 + breakOnConsoleErrors: castBool(breakOnConsoleErrors) ?? false,
63 + showInlineWarningsAndErrors: castBool(showInlineWarningsAndErrors) ?? true,
64 + hideConsoleLogsInStrictMode: castBool(hideConsoleLogsInStrictMode) ?? false,
65 + browserTheme: castBrowserTheme(browserTheme) ?? 'dark',
66 + };
67 +}
68 +
69 +export function cacheConsolePatchSettings(
70 + devToolsSettingsManager: DevToolsSettingsManager,
71 + value: ConsolePatchSettings,
72 +): void {
73 + if (devToolsSettingsManager.setConsolePatchSettings == null) {
74 + return;
75 + }
76 + devToolsSettingsManager.setConsolePatchSettings(JSON.stringify(value));
77 +}
packages/react-devtools-shared/src/backend/agent.js
+5 -5
@@ -25,7 +25,7 @@ import {
25 initialize as setupTraceUpdates,
26 toggleEnabled as setTraceUpdatesEnabled,
27 } from './views/TraceUpdates';
28 -import {patch as patchConsole} from './console';
28 +import {patch as patchConsole, type ConsolePatchSettings} from './console';
29 import {currentBridgeProtocol} from 'react-devtools-shared/src/bridge';
30
31 import type {BackendBridge} from 'react-devtools-shared/src/bridge';
@@ -712,11 +712,11 @@ export default class Agent extends EventEmitter<{
712 showInlineWarningsAndErrors,
713 hideConsoleLogsInStrictMode,
714 browserTheme,
715 - }) => {
716 - // If the frontend preference has change,
717 - // or in the case of React Native- if the backend is just finding out the preference-
715 + }: ConsolePatchSettings) => {
716 + // If the frontend preferences have changed,
717 + // or in the case of React Native- if the backend is just finding out the preferences-
718 // then reinstall the console overrides.
719 - // It's safe to call these methods multiple times, so we don't need to worry about that.
719 + // It's safe to call `patchConsole` multiple times.
720 patchConsole({
721 appendComponentStack,
722 breakOnConsoleErrors,
packages/react-devtools-shared/src/backend/console.js
+25 -17
@@ -15,6 +15,7 @@ import {format, formatWithStyles} from './utils';
15 import {getInternalReactConstants} from './renderer';
16 import {getStackByFiberInDevAndProd} from './DevToolsFiberComponentStack';
17 import {consoleManagedByDevToolsDuringStrictMode} from 'react-devtools-feature-flags';
18 +import {castBool, castBrowserTheme} from '../utils';
19
20 const OVERRIDE_CONSOLE_METHODS = ['error', 'trace', 'warn'];
21 const DIMMED_NODE_CONSOLE_COLOR = '\x1b[2m%s\x1b[0m';
@@ -143,6 +144,14 @@ const consoleSettingsRef = {
144 browserTheme: 'dark',
145 };
146
147 +export type ConsolePatchSettings = {
148 + appendComponentStack: boolean,
149 + breakOnConsoleErrors: boolean,
150 + showInlineWarningsAndErrors: boolean,
151 + hideConsoleLogsInStrictMode: boolean,
152 + browserTheme: BrowserTheme,
153 +};
154 +
155 // Patches console methods to append component stack for the current fiber.
156 // Call unpatch() to remove the injected behavior.
157 export function patch({
@@ -151,13 +160,7 @@ export function patch({
160 showInlineWarningsAndErrors,
161 hideConsoleLogsInStrictMode,
162 browserTheme,
154 -}: {
155 - appendComponentStack: boolean,
156 - breakOnConsoleErrors: boolean,
157 - showInlineWarningsAndErrors: boolean,
158 - hideConsoleLogsInStrictMode: boolean,
159 - browserTheme: BrowserTheme,
160 -}): void {
163 +}: ConsolePatchSettings): void {
164 // Settings may change after we've patched the console.
165 // Using a shared ref allows the patch function to read the latest values.
166 consoleSettingsRef.appendComponentStack = appendComponentStack;
@@ -390,14 +393,19 @@ export function patchConsoleUsingWindowValues() {
393 });
394 }
395
393 -function castBool(v: any): ?boolean {
394 - if (v === true || v === false) {
395 - return v;
396 - }
397 -}
398 -
399 -function castBrowserTheme(v: any): ?BrowserTheme {
400 - if (v === 'light' || v === 'dark' || v === 'auto') {
401 - return v;
402 - }
396 +// After receiving cached console patch settings from React Native, we set them on window.
397 +// When the console is initially patched (in renderer.js and hook.js), these values are read.
398 +// The browser extension (etc.) sets these values on window, but through another method.
399 +export function writeConsolePatchSettingsToWindow(
400 + settings: ConsolePatchSettings,
401 +): void {
402 + window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ =
403 + settings.appendComponentStack;
404 + window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ =
405 + settings.breakOnConsoleErrors;
406 + window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ =
407 + settings.showInlineWarningsAndErrors;
408 + window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ =
409 + settings.hideConsoleLogsInStrictMode;
410 + window.__REACT_DEVTOOLS_BROWSER_THEME__ = settings.browserTheme;
411 }
packages/react-devtools-shared/src/bridge.js
+2 -10
@@ -17,7 +17,7 @@ import type {
17 RendererID,
18 } from 'react-devtools-shared/src/backend/types';
19 import type {StyleAndLayout as StyleAndLayoutPayload} from 'react-devtools-shared/src/backend/NativeStyleEditor/types';
20 -import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
20 +import type {ConsolePatchSettings} from 'react-devtools-shared/src/backend/console';
21
22 const BATCH_DURATION = 100;
23
@@ -171,14 +171,6 @@ type NativeStyleEditor_SetValueParams = {
171 value: string,
172 };
173
174 -type UpdateConsolePatchSettingsParams = {
175 - appendComponentStack: boolean,
176 - breakOnConsoleErrors: boolean,
177 - showInlineWarningsAndErrors: boolean,
178 - hideConsoleLogsInStrictMode: boolean,
179 - browserTheme: BrowserTheme,
180 -};
181 -
174 type SavedPreferencesParams = {
175 appendComponentStack: boolean,
176 breakOnConsoleErrors: boolean,
@@ -247,7 +239,7 @@ type FrontendEvents = {
239 stopProfiling: [],
240 storeAsGlobal: [StoreAsGlobalParams],
241 updateComponentFilters: [Array<ComponentFilter>],
250 - updateConsolePatchSettings: [UpdateConsolePatchSettingsParams],
242 + updateConsolePatchSettings: [ConsolePatchSettings],
243 viewAttributeSource: [ViewAttributeSourceParams],
244 viewElementSource: [ElementAndRendererID],
245
packages/react-devtools-shared/src/utils.js
+13
@@ -55,6 +55,7 @@ import isArray from './isArray';
55
56 import type {ComponentFilter, ElementType} from './types';
57 import type {LRUCache} from 'react-devtools-shared/src/types';
58 +import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
59
60 // $FlowFixMe[method-unbinding]
61 const hasOwnProperty = Object.prototype.hasOwnProperty;
@@ -353,6 +354,18 @@ function parseBool(s: ?string): ?boolean {
354 }
355 }
356
357 +export function castBool(v: any): ?boolean {
358 + if (v === true || v === false) {
359 + return v;
360 + }
361 +}
362 +
363 +export function castBrowserTheme(v: any): ?BrowserTheme {
364 + if (v === 'light' || v === 'dark' || v === 'auto') {
365 + return v;
366 + }
367 +}
368 +
369 export function getAppendComponentStack(): boolean {
370 const raw = localStorageGetItem(
371 LOCAL_STORAGE_SHOULD_APPEND_COMPONENT_STACK_KEY,