mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48054 Web debugging should be fully removed as of recent, so lets remove some of the gunk that was powering it, and checks for that environment. I did some string searches for the following: * isAsyncDebugging * debugRemotely * isDebuggingRemotely * isRemoteDebuggingAvailable * WebSocketExecutor * JavaJSExecutor * ProxyJavaScriptExecutor * RELOAD_APP_EXTRA_JS_PROXY * getJSBundleURLForRemoteDebugging * onReloadWithJSDebugger * setRemoteJSDebugEnabled * WebsocketJavaScriptExecutor * createRemoteDebuggerBundleLoader 1. `expo-modules-core` exposes its own `isAsyncDebugging` by checking for `nativeCallSyncHook`, but does not depend on `DebugEnvironment`. 2. `expo-dev-menu` does read `isDebuggingRemotely` from `DevSettings`. 3. Realm does a check in Native using `objc_lookUpClass("RCTWebSocketExecutor")` but will gracefully handle `nil` if it does not exist 4. Some more usages (e.g. `onReloadWithJSDebugger`) in vendored packages of `expo-dev-launcher` for RN 0.74 I created an issue for Expo mentioning both here: https://github.com/expo/expo/issues/33371 Changelog: [General][Breaking] - Remove some web debugging remnants Reviewed By: huntie Differential Revision: D66553934 fbshipit-source-id: deec382b1c8bda393fddb8682aa91b26afd9fbe3
126 lines
3.9 KiB
JavaScript
126 lines
3.9 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow strict-local
|
|
*/
|
|
|
|
import type {EventSubscription} from '../vendor/emitter/EventEmitter';
|
|
import type {AppearancePreferences, ColorSchemeName} from './NativeAppearance';
|
|
import typeof INativeAppearance from './NativeAppearance';
|
|
|
|
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
|
|
import EventEmitter from '../vendor/emitter/EventEmitter';
|
|
import invariant from 'invariant';
|
|
|
|
type Appearance = {
|
|
colorScheme: ?ColorSchemeName,
|
|
};
|
|
|
|
let lazyState: ?{
|
|
+NativeAppearance: INativeAppearance,
|
|
// Cache the color scheme to reduce the cost of reading it between changes.
|
|
// NOTE: If `NativeAppearance` is null, this will always be null.
|
|
appearance: ?Appearance,
|
|
// NOTE: This is non-nullable to make it easier for `onChangedListener` to
|
|
// return a non-nullable `EventSubscription` value. This is not the common
|
|
// path, so we do not have to over-optimize it.
|
|
+eventEmitter: EventEmitter<{change: [Appearance]}>,
|
|
};
|
|
|
|
/**
|
|
* Ensures that all state and listeners are lazily initialized correctly.
|
|
*/
|
|
function getState(): $NonMaybeType<typeof lazyState> {
|
|
if (lazyState != null) {
|
|
return lazyState;
|
|
}
|
|
const eventEmitter = new EventEmitter<{change: [Appearance]}>();
|
|
// NOTE: Avoid initializing `NativeAppearance` until it is actually used.
|
|
const NativeAppearance = require('./NativeAppearance').default;
|
|
if (NativeAppearance == null) {
|
|
// Assign `null` to avoid re-initializing on subsequent invocations.
|
|
lazyState = {
|
|
NativeAppearance: null,
|
|
appearance: null,
|
|
eventEmitter,
|
|
};
|
|
} else {
|
|
const state: $NonMaybeType<typeof lazyState> = {
|
|
NativeAppearance,
|
|
appearance: null,
|
|
eventEmitter,
|
|
};
|
|
new NativeEventEmitter<{
|
|
appearanceChanged: [AppearancePreferences],
|
|
}>(NativeAppearance).addListener('appearanceChanged', newAppearance => {
|
|
state.appearance = {
|
|
colorScheme: toColorScheme(newAppearance.colorScheme),
|
|
};
|
|
eventEmitter.emit('change', state.appearance);
|
|
});
|
|
lazyState = state;
|
|
}
|
|
return lazyState;
|
|
}
|
|
|
|
/**
|
|
* Returns the current color scheme preference. This value may change, so the
|
|
* value should not be cached without either listening to changes or using
|
|
* the `useColorScheme` hook.
|
|
*/
|
|
export function getColorScheme(): ?ColorSchemeName {
|
|
let colorScheme = null;
|
|
const state = getState();
|
|
const {NativeAppearance} = state;
|
|
if (NativeAppearance != null) {
|
|
if (state.appearance == null) {
|
|
// Lazily initialize `state.appearance`. This should only
|
|
// happen once because we never reassign a null value to it.
|
|
state.appearance = {
|
|
colorScheme: toColorScheme(NativeAppearance.getColorScheme()),
|
|
};
|
|
}
|
|
colorScheme = state.appearance.colorScheme;
|
|
}
|
|
return colorScheme;
|
|
}
|
|
|
|
/**
|
|
* Updates the current color scheme to the supplied value.
|
|
*/
|
|
export function setColorScheme(colorScheme: ?ColorSchemeName): void {
|
|
const state = getState();
|
|
const {NativeAppearance} = state;
|
|
if (NativeAppearance != null) {
|
|
NativeAppearance.setColorScheme(colorScheme ?? 'unspecified');
|
|
state.appearance = {
|
|
colorScheme: toColorScheme(NativeAppearance.getColorScheme()),
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add an event handler that is fired when appearance preferences change.
|
|
*/
|
|
export function addChangeListener(
|
|
listener: ({colorScheme: ?ColorSchemeName}) => void,
|
|
): EventSubscription {
|
|
const {eventEmitter} = getState();
|
|
return eventEmitter.addListener('change', listener);
|
|
}
|
|
|
|
/**
|
|
* TODO: (hramos) T52919652 Use ?ColorSchemeName once codegen supports union
|
|
*/
|
|
function toColorScheme(colorScheme: ?string): ?ColorSchemeName {
|
|
invariant(
|
|
colorScheme === 'dark' || colorScheme === 'light' || colorScheme == null,
|
|
"Unrecognized color scheme. Did you mean 'dark', 'light' or null?",
|
|
);
|
|
return colorScheme;
|
|
}
|