Skip to content

Commit 8f447a2

Browse files
rbalicki2rickhanlonii
authored andcommitted
[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 facebook/react-native#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.
1 parent 544412b commit 8f447a2

File tree

6 files changed

+148
-32
lines changed

6 files changed

+148
-32
lines changed

packages/react-devtools-core/src/backend.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import {initBackend} from 'react-devtools-shared/src/backend';
1414
import {__DEBUG__} from 'react-devtools-shared/src/constants';
1515
import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor';
1616
import {getDefaultComponentFilters} from 'react-devtools-shared/src/utils';
17+
import {
18+
initializeUsingCachedSettings,
19+
cacheConsolePatchSettings,
20+
type DevToolsSettingsManager,
21+
} from './cachedSettings';
1722

1823
import type {BackendBridge} from 'react-devtools-shared/src/bridge';
1924
import type {ComponentFilter} from 'react-devtools-shared/src/types';
@@ -29,6 +34,7 @@ type ConnectOptions = {
2934
retryConnectionDelay?: number,
3035
isAppActive?: () => boolean,
3136
websocket?: ?WebSocket,
37+
devToolsSettingsManager: ?DevToolsSettingsManager,
3238
...
3339
};
3440

@@ -63,6 +69,7 @@ export function connectToDevTools(options: ?ConnectOptions) {
6369
resolveRNStyle = null,
6470
retryConnectionDelay = 2000,
6571
isAppActive = () => true,
72+
devToolsSettingsManager,
6673
} = options || {};
6774

6875
const protocol = useHttps ? 'wss' : 'ws';
@@ -78,6 +85,16 @@ export function connectToDevTools(options: ?ConnectOptions) {
7885
}
7986
}
8087

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+
8198
if (!isAppActive()) {
8299
// If the app is in background, maybe retry later.
83100
// Don't actually attempt to connect until we're in foreground.
@@ -142,6 +159,15 @@ export function connectToDevTools(options: ?ConnectOptions) {
142159
},
143160
);
144161

162+
if (devToolsSettingsManager != null && bridge != null) {
163+
bridge.addListener('updateConsolePatchSettings', consolePatchSettings =>
164+
cacheConsolePatchSettings(
165+
devToolsSettingsManager,
166+
consolePatchSettings,
167+
),
168+
);
169+
}
170+
145171
// The renderer interface doesn't read saved component filters directly,
146172
// because they are generally stored in localStorage within the context of the extension.
147173
// Because of this it relies on the extension to pass filters.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
initialize as setupTraceUpdates,
2626
toggleEnabled as setTraceUpdatesEnabled,
2727
} from './views/TraceUpdates';
28-
import {patch as patchConsole} from './console';
28+
import {patch as patchConsole, type ConsolePatchSettings} from './console';
2929
import {currentBridgeProtocol} from 'react-devtools-shared/src/bridge';
3030

3131
import type {BackendBridge} from 'react-devtools-shared/src/bridge';
@@ -712,11 +712,11 @@ export default class Agent extends EventEmitter<{
712712
showInlineWarningsAndErrors,
713713
hideConsoleLogsInStrictMode,
714714
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-
718718
// 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.
720720
patchConsole({
721721
appendComponentStack,
722722
breakOnConsoleErrors,

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {format, formatWithStyles} from './utils';
1515
import {getInternalReactConstants} from './renderer';
1616
import {getStackByFiberInDevAndProd} from './DevToolsFiberComponentStack';
1717
import {consoleManagedByDevToolsDuringStrictMode} from 'react-devtools-feature-flags';
18+
import {castBool, castBrowserTheme} from '../utils';
1819

1920
const OVERRIDE_CONSOLE_METHODS = ['error', 'trace', 'warn'];
2021
const DIMMED_NODE_CONSOLE_COLOR = '\x1b[2m%s\x1b[0m';
@@ -143,6 +144,14 @@ const consoleSettingsRef = {
143144
browserTheme: 'dark',
144145
};
145146

147+
export type ConsolePatchSettings = {
148+
appendComponentStack: boolean,
149+
breakOnConsoleErrors: boolean,
150+
showInlineWarningsAndErrors: boolean,
151+
hideConsoleLogsInStrictMode: boolean,
152+
browserTheme: BrowserTheme,
153+
};
154+
146155
// Patches console methods to append component stack for the current fiber.
147156
// Call unpatch() to remove the injected behavior.
148157
export function patch({
@@ -151,13 +160,7 @@ export function patch({
151160
showInlineWarningsAndErrors,
152161
hideConsoleLogsInStrictMode,
153162
browserTheme,
154-
}: {
155-
appendComponentStack: boolean,
156-
breakOnConsoleErrors: boolean,
157-
showInlineWarningsAndErrors: boolean,
158-
hideConsoleLogsInStrictMode: boolean,
159-
browserTheme: BrowserTheme,
160-
}): void {
163+
}: ConsolePatchSettings): void {
161164
// Settings may change after we've patched the console.
162165
// Using a shared ref allows the patch function to read the latest values.
163166
consoleSettingsRef.appendComponentStack = appendComponentStack;
@@ -390,14 +393,19 @@ export function patchConsoleUsingWindowValues() {
390393
});
391394
}
392395

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;
403411
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
RendererID,
1818
} from 'react-devtools-shared/src/backend/types';
1919
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';
2121

2222
const BATCH_DURATION = 100;
2323

@@ -171,14 +171,6 @@ type NativeStyleEditor_SetValueParams = {
171171
value: string,
172172
};
173173

174-
type UpdateConsolePatchSettingsParams = {
175-
appendComponentStack: boolean,
176-
breakOnConsoleErrors: boolean,
177-
showInlineWarningsAndErrors: boolean,
178-
hideConsoleLogsInStrictMode: boolean,
179-
browserTheme: BrowserTheme,
180-
};
181-
182174
type SavedPreferencesParams = {
183175
appendComponentStack: boolean,
184176
breakOnConsoleErrors: boolean,
@@ -247,7 +239,7 @@ type FrontendEvents = {
247239
stopProfiling: [],
248240
storeAsGlobal: [StoreAsGlobalParams],
249241
updateComponentFilters: [Array<ComponentFilter>],
250-
updateConsolePatchSettings: [UpdateConsolePatchSettingsParams],
242+
updateConsolePatchSettings: [ConsolePatchSettings],
251243
viewAttributeSource: [ViewAttributeSourceParams],
252244
viewElementSource: [ElementAndRendererID],
253245

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import isArray from './isArray';
5555

5656
import type {ComponentFilter, ElementType} from './types';
5757
import type {LRUCache} from 'react-devtools-shared/src/types';
58+
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
5859

5960
// $FlowFixMe[method-unbinding]
6061
const hasOwnProperty = Object.prototype.hasOwnProperty;
@@ -353,6 +354,18 @@ function parseBool(s: ?string): ?boolean {
353354
}
354355
}
355356

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+
356369
export function getAppendComponentStack(): boolean {
357370
const raw = localStorageGetItem(
358371
LOCAL_STORAGE_SHOULD_APPEND_COMPONENT_STACK_KEY,

0 commit comments

Comments
 (0)