-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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,80 @@ | ||
/** | ||
* 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'; | ||
|
||
export type CachedSettingsStore = { | ||
setValue: (key: string, value: string) => void, | ||
getValue: (key: string) => ?string, | ||
}; | ||
|
||
export type CachedSettings = { | ||
consolePatchSettings: ?ConsolePatchSettings, | ||
}; | ||
|
||
const CONSOLE_PATCH_SETTINGS_KEY = 'ConsolePatchSettings'; | ||
|
||
export function initializeUsingCachedSettings( | ||
cachedSettingsStore: CachedSettingsStore, | ||
) { | ||
const cachedSettingsString = cachedSettingsStore.getValue( | ||
CONSOLE_PATCH_SETTINGS_KEY, | ||
); | ||
if (cachedSettingsString == null) { | ||
return; | ||
} | ||
const cachedSettings = parseCachedSettings(cachedSettingsString); | ||
if (cachedSettings != null && cachedSettings.consolePatchSettings != null) { | ||
writeConsolePatchSettingsToWindow(cachedSettings.consolePatchSettings); | ||
} | ||
} | ||
|
||
function parseCachedSettings(cachedSettingsString: string): ?CachedSettings { | ||
const parsedValue = JSON.parse(cachedSettingsString); | ||
|
||
if (typeof parsedValue === 'object' && parsedValue != null) { | ||
const consolePatchSettings = parseConsolePatchSettings( | ||
parsedValue.consolePatchSettings, | ||
); | ||
return { | ||
consolePatchSettings, | ||
}; | ||
} | ||
} | ||
|
||
function parseConsolePatchSettings(value: any): ?ConsolePatchSettings { | ||
if (typeof value !== 'object' || value === null) { | ||
return; | ||
} | ||
const { | ||
appendComponentStack, | ||
breakOnConsoleErrors, | ||
showInlineWarningsAndErrors, | ||
hideConsoleLogsInStrictMode, | ||
browserTheme, | ||
} = value; | ||
return { | ||
appendComponentStack: castBool(appendComponentStack) ?? true, | ||
breakOnConsoleErrors: castBool(breakOnConsoleErrors) ?? false, | ||
showInlineWarningsAndErrors: castBool(showInlineWarningsAndErrors) ?? true, | ||
hideConsoleLogsInStrictMode: castBool(hideConsoleLogsInStrictMode) ?? false, | ||
browserTheme: castBrowserTheme(browserTheme) ?? 'dark', | ||
}; | ||
} | ||
|
||
export function cacheSettings( | ||
cachedSettings: CachedSettingsStore, | ||
value: CachedSettings, | ||
): void { | ||
cachedSettings.setValue(CONSOLE_PATCH_SETTINGS_KEY, 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