Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[DebuggingRegistry]: basic implementation #41743

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/react-native/Libraries/Debugging/DebuggingRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.
*
* @flow strict-local
* @format
* @oncall react_native
*/

import type {AppContainerRootViewRef} from '../ReactNative/AppContainer-dev';

export type DebuggingRegistrySubscriberProtocol = {
rootViewRef: AppContainerRootViewRef,
};

class DebuggingRegistry {
#registry: Set<DebuggingRegistrySubscriberProtocol> = new Set();

subscribe(subscriber: DebuggingRegistrySubscriberProtocol) {
this.#registry.add(subscriber);
}

unsubscribe(subscriber: DebuggingRegistrySubscriberProtocol) {
const wasPresent = this.#registry.delete(subscriber);
if (!wasPresent) {
console.error(
'[DebuggingRegistry] Unexpected argument for unsubscription, which was not previously subscribed:',
subscriber,
);
}
}
}

const debuggingRegistryInstance: DebuggingRegistry = new DebuggingRegistry();
export default debuggingRegistryInstance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.
*
* @flow strict-local
* @format
* @oncall react_native
*/

import type {AppContainerRootViewRef} from '../ReactNative/AppContainer-dev';

import DebuggingRegistry from './DebuggingRegistry';
import {useEffect} from 'react';

const useSubscribeToDebuggingRegistry = (
rootViewRef: AppContainerRootViewRef,
) => {
useEffect(() => {
const subscriber = {rootViewRef};

DebuggingRegistry.subscribe(subscriber);
return () => DebuggingRegistry.unsubscribe(subscriber);
}, [rootViewRef]);
};

export default useSubscribeToDebuggingRegistry;
3 changes: 2 additions & 1 deletion packages/react-native/Libraries/Inspector/Inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

import type {InspectedViewRef} from '../ReactNative/AppContainer-dev';
import type {
InspectorData,
TouchedViewDataAtPoint,
Expand Down Expand Up @@ -46,7 +47,7 @@ export type InspectedElement = $ReadOnly<{
export type ElementsHierarchy = InspectorData['hierarchy'];

type Props = {
inspectedViewRef: React.RefObject<React.ElementRef<typeof View> | null>,
inspectedViewRef: InspectedViewRef,
onRequestRerenderApp: () => void,
reactDevToolsAgent?: ReactDevToolsAgent,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @flow
*/

import type {InspectedViewRef} from '../ReactNative/AppContainer-dev';
import type {PointerEvent} from '../Types/CoreEventTypes';
import type {PressEvent} from '../Types/CoreEventTypes';
import type {
Expand All @@ -29,7 +30,7 @@ const getInspectorDataForViewAtPoint = require('./getInspectorDataForViewAtPoint
const {useEffect, useState, useCallback} = React;

type Props = {
inspectedViewRef: React.RefObject<React.ElementRef<typeof View> | null>,
inspectedViewRef: InspectedViewRef,
reactDevToolsAgent: ReactDevToolsAgent,
};

Expand Down
18 changes: 15 additions & 3 deletions packages/react-native/Libraries/ReactNative/AppContainer-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {Props} from './AppContainer';
import TraceUpdateOverlay from '../Components/TraceUpdateOverlay/TraceUpdateOverlay';
import ReactNativeStyleAttributes from '../Components/View/ReactNativeStyleAttributes';
import View from '../Components/View/View';
import useSubscribeToDebuggingRegistry from '../Debugging/useSubscribeToDebuggingRegistry';
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
import ReactDevToolsOverlay from '../Inspector/ReactDevToolsOverlay';
import LogBoxNotificationContainer from '../LogBox/LogBoxNotificationContainer';
Expand All @@ -40,7 +41,7 @@ if (reactDevToolsHook) {
}

type InspectorDeferredProps = {
inspectedViewRef: React.RefObject<React.ElementRef<typeof View> | null>,
inspectedViewRef: InspectedViewRef,
onInspectedViewRerenderRequest: () => void,
reactDevToolsAgent?: ReactDevToolsAgent,
};
Expand Down Expand Up @@ -73,7 +74,9 @@ const AppContainer = ({
showArchitectureIndicator,
WrapperComponent,
}: Props): React.Node => {
const innerViewRef = React.useRef<React.ElementRef<typeof View> | null>(null);
const appContainerRootViewRef: AppContainerRootViewRef = React.useRef(null);
const innerViewRef: InspectedViewRef = React.useRef(null);
useSubscribeToDebuggingRegistry(appContainerRootViewRef);

const [key, setKey] = useState(0);
const [shouldRenderInspector, setShouldRenderInspector] = useState(false);
Expand Down Expand Up @@ -138,7 +141,10 @@ const AppContainer = ({

return (
<RootTagContext.Provider value={createRootTag(rootTag)}>
<View style={styles.container} pointerEvents="box-none">
<View
ref={appContainerRootViewRef}
style={styles.container}
pointerEvents="box-none">
{innerView}

{reactDevToolsAgent != null && (
Expand Down Expand Up @@ -169,4 +175,10 @@ const styles = StyleSheet.create({
container: {flex: 1},
});

export type AppContainerRootViewRef = React.RefObject<React.ElementRef<
typeof View,
> | null>;
export type InspectedViewRef = React.RefObject<React.ElementRef<
typeof View,
> | null>;
export default AppContainer;
Loading