-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
Showing
6 changed files
with
148 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* 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 ConsolePatchSettings, | ||
writeConsolePatchSettingsToWindow, | ||
} from 'react-devtools-shared/src/backend/console'; | ||
import {castBool, castBrowserTheme} 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, | ||
browserTheme, | ||
} = parsedValue; | ||
return { | ||
appendComponentStack: castBool(appendComponentStack) ?? true, | ||
breakOnConsoleErrors: castBool(breakOnConsoleErrors) ?? false, | ||
showInlineWarningsAndErrors: castBool(showInlineWarningsAndErrors) ?? true, | ||
hideConsoleLogsInStrictMode: castBool(hideConsoleLogsInStrictMode) ?? false, | ||
browserTheme: castBrowserTheme(browserTheme) ?? 'dark', | ||
}; | ||
} | ||
|
||
export function cacheConsolePatchSettings( | ||
devToolsSettingsManager: DevToolsSettingsManager, | ||
value: ConsolePatchSettings, | ||
): void { | ||
if (devToolsSettingsManager.setConsolePatchSettings == null) { | ||
return; | ||
} | ||
devToolsSettingsManager.setConsolePatchSettings(JSON.stringify(value)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters