Skip to content

Commit

Permalink
Merge SurfaceRegistry into AppRegistry (facebook#37381)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#37381

SurfaceRegistry was a bridgeless-only concept, which hasn't actually diverged much from AppRegistry and doesn't support any new use-cases. It also causes additional complexity in terms of Logbox. Merge the logic with AppRegistry, and rewrite existing callers to AppRegistry.

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D45730959

fbshipit-source-id: e2e2626c4dec8423aa097eff76cfa4d199f3a680
  • Loading branch information
javache authored and facebook-github-bot committed May 18, 2023
1 parent 75d58d4 commit fd92341
Showing 1 changed file with 65 additions and 52 deletions.
117 changes: 65 additions & 52 deletions packages/react-native/Libraries/ReactNative/AppRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import type {RootTag} from '../Types/RootTagTypes';
import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
import type {DisplayModeType} from './DisplayMode';

import BatchedBridge from '../BatchedBridge/BatchedBridge';
import BugReporting from '../BugReporting/BugReporting';
Expand All @@ -36,23 +37,28 @@ export type ComponentProviderInstrumentationHook = (
export type AppConfig = {
appKey: string,
component?: ComponentProvider,
run?: Function,
run?: Runnable,
section?: boolean,
...
};
export type Runnable = {
component?: ComponentProvider,
run: Function,
...
type AppParameters = {
initialProps: $ReadOnly<{[string]: mixed, ...}>,
rootTag: RootTag,
fabric?: boolean,
concurrentRoot?: boolean,
};
export type Runnables = {[appKey: string]: Runnable, ...};
export type Runnable = (
appParameters: AppParameters,
displayMode: DisplayModeType,
) => void;
export type Runnables = {[appKey: string]: Runnable};
export type Registry = {
sections: Array<string>,
sections: $ReadOnlyArray<string>,
runnables: Runnables,
...
};
export type WrapperComponentProvider = (
appParameters: any,
appParameters: Object,
) => React$ComponentType<any>;

const runnables: Runnables = {};
Expand Down Expand Up @@ -110,51 +116,49 @@ const AppRegistry = {
componentProvider: ComponentProvider,
section?: boolean,
): string {
let scopedPerformanceLogger = createPerformanceLogger();
runnables[appKey] = {
componentProvider,
run: (appParameters, displayMode) => {
const concurrentRootEnabled =
appParameters.initialProps?.concurrentRoot ||
appParameters.concurrentRoot;
renderApplication(
componentProviderInstrumentationHook(
componentProvider,
scopedPerformanceLogger,
),
appParameters.initialProps,
appParameters.rootTag,
wrapperComponentProvider && wrapperComponentProvider(appParameters),
appParameters.fabric,
showArchitectureIndicator,
const scopedPerformanceLogger = createPerformanceLogger();
runnables[appKey] = (appParameters, displayMode) => {
const concurrentRootEnabled = Boolean(
appParameters.initialProps?.concurrentRoot ||
appParameters.concurrentRoot,
);
renderApplication(
componentProviderInstrumentationHook(
componentProvider,
scopedPerformanceLogger,
appKey === 'LogBox',
appKey,
coerceDisplayMode(displayMode),
concurrentRootEnabled,
);
},
),
appParameters.initialProps,
appParameters.rootTag,
wrapperComponentProvider && wrapperComponentProvider(appParameters),
appParameters.fabric,
showArchitectureIndicator,
scopedPerformanceLogger,
appKey === 'LogBox', // is logbox
appKey,
displayMode,
concurrentRootEnabled,
);
};
if (section) {
sections[appKey] = runnables[appKey];
}
return appKey;
},

registerRunnable(appKey: string, run: Function): string {
runnables[appKey] = {run};
registerRunnable(appKey: string, run: Runnable): string {
runnables[appKey] = run;
return appKey;
},

registerSection(appKey: string, component: ComponentProvider): void {
AppRegistry.registerComponent(appKey, component, true);
},

getAppKeys(): Array<string> {
getAppKeys(): $ReadOnlyArray<string> {
return Object.keys(runnables);
},

getSectionKeys(): Array<string> {
getSectionKeys(): $ReadOnlyArray<string> {
return Object.keys(sections);
},

Expand Down Expand Up @@ -188,7 +192,7 @@ const AppRegistry = {
*/
runApplication(
appKey: string,
appParameters: any,
appParameters: AppParameters,
displayMode?: number,
): void {
if (appKey !== 'LogBox') {
Expand All @@ -203,23 +207,23 @@ const AppRegistry = {
);
}
invariant(
runnables[appKey] && runnables[appKey].run,
runnables[appKey],
`"${appKey}" has not been registered. This can happen if:\n` +
'* Metro (the local dev server) is run from the wrong folder. ' +
'Check if Metro is running, stop it and restart it in the current project.\n' +
"* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.",
);

SceneTracker.setActiveScene({name: appKey});
runnables[appKey].run(appParameters, displayMode);
runnables[appKey](appParameters, coerceDisplayMode(displayMode));
},

/**
* Update initial props for a surface that's already rendered
*/
setSurfaceProps(
appKey: string,
appParameters: any,
appParameters: Object,
displayMode?: number,
): void {
if (appKey !== 'LogBox') {
Expand All @@ -235,14 +239,14 @@ const AppRegistry = {
);
}
invariant(
runnables[appKey] && runnables[appKey].run,
runnables[appKey],
`"${appKey}" has not been registered. This can happen if:\n` +
'* Metro (the local dev server) is run from the wrong folder. ' +
'Check if Metro is running, stop it and restart it in the current project.\n' +
"* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.",
);

runnables[appKey].run(appParameters, displayMode);
runnables[appKey](appParameters, coerceDisplayMode(displayMode));
},

/**
Expand Down Expand Up @@ -338,18 +342,27 @@ const AppRegistry = {
},
};

if (!(global.RN$Bridgeless === true)) {
BatchedBridge.registerCallableModule('AppRegistry', AppRegistry);
// Register LogBox as a default surface
AppRegistry.registerComponent('LogBox', () => {
if (__DEV__ && typeof jest === 'undefined') {
return require('../LogBox/LogBoxInspectorContainer').default;
} else {
return function NoOp() {
return null;
};
}
});

AppRegistry.registerComponent('LogBox', () => {
if (__DEV__) {
return require('../LogBox/LogBoxInspectorContainer').default;
} else {
return function NoOp() {
return null;
};
}
});
global.RN$AppRegistry = AppRegistry;

// Backwards compat with SurfaceRegistry, remove me later
global.RN$SurfaceRegistry = {
renderSurface: AppRegistry.runApplication,
setSurfaceProps: AppRegistry.setSurfaceProps,
};

if (global.RN$Bridgeless !== true) {
BatchedBridge.registerCallableModule('AppRegistry', AppRegistry);
}

module.exports = AppRegistry;

0 comments on commit fd92341

Please sign in to comment.