mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Refactor AppRegistry to generate a TS namespace (#49882)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49882 Changelog: [Internal] Reviewed By: huntie Differential Revision: D70627137 fbshipit-source-id: a68524bdd6481f1df2a3529c72f0b5e5b17c521b
This commit is contained in:
1 parent
fef7933f63
commit
d3a27f27a0
6 files changed
+482
-367
No files matched your search
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* 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
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
|
||||
import type {RootTag} from '../Types/RootTagTypes';
|
||||
import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
|
||||
import type {DisplayModeType} from './DisplayMode';
|
||||
|
||||
type Task = (taskData: any) => Promise<void>;
|
||||
export type TaskProvider = () => Task;
|
||||
|
||||
export type ComponentProvider = () => React.ComponentType<any>;
|
||||
export type ComponentProviderInstrumentationHook = (
|
||||
component_: ComponentProvider,
|
||||
scopedPerformanceLogger: IPerformanceLogger,
|
||||
) => React.ComponentType<any>;
|
||||
export type AppConfig = {
|
||||
appKey: string,
|
||||
component?: ComponentProvider,
|
||||
run?: Runnable,
|
||||
section?: boolean,
|
||||
...
|
||||
};
|
||||
export type AppParameters = {
|
||||
initialProps: $ReadOnly<{[string]: mixed, ...}>,
|
||||
rootTag: RootTag,
|
||||
fabric?: boolean,
|
||||
};
|
||||
export type Runnable = (
|
||||
appParameters: AppParameters,
|
||||
displayMode: DisplayModeType,
|
||||
) => void;
|
||||
export type Runnables = {[appKey: string]: Runnable};
|
||||
export type Registry = {
|
||||
sections: $ReadOnlyArray<string>,
|
||||
runnables: Runnables,
|
||||
...
|
||||
};
|
||||
export type WrapperComponentProvider = (
|
||||
appParameters: Object,
|
||||
) => React.ComponentType<any>;
|
||||
export type RootViewStyleProvider = (appParameters: Object) => ViewStyleProp;
|
||||
+2
-321
@@ -8,333 +8,13 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
|
||||
import type {RootTag} from '../Types/RootTagTypes';
|
||||
import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
|
||||
import type {DisplayModeType} from './DisplayMode';
|
||||
|
||||
import BugReporting from '../BugReporting/BugReporting';
|
||||
import registerCallableModule from '../Core/registerCallableModule';
|
||||
import createPerformanceLogger from '../Utilities/createPerformanceLogger';
|
||||
import infoLog from '../Utilities/infoLog';
|
||||
import SceneTracker from '../Utilities/SceneTracker';
|
||||
import {coerceDisplayMode} from './DisplayMode';
|
||||
import HeadlessJsTaskError from './HeadlessJsTaskError';
|
||||
import NativeHeadlessJsTaskSupport from './NativeHeadlessJsTaskSupport';
|
||||
import renderApplication from './renderApplication';
|
||||
import {unmountComponentAtNodeAndRemoveContainer} from './RendererProxy';
|
||||
import invariant from 'invariant';
|
||||
|
||||
type Task = (taskData: any) => Promise<void>;
|
||||
export type TaskProvider = () => Task;
|
||||
type TaskCanceller = () => void;
|
||||
type TaskCancelProvider = () => TaskCanceller;
|
||||
|
||||
export type ComponentProvider = () => React.ComponentType<any>;
|
||||
export type ComponentProviderInstrumentationHook = (
|
||||
component_: ComponentProvider,
|
||||
scopedPerformanceLogger: IPerformanceLogger,
|
||||
) => React.ComponentType<any>;
|
||||
export type AppConfig = {
|
||||
appKey: string,
|
||||
component?: ComponentProvider,
|
||||
run?: Runnable,
|
||||
section?: boolean,
|
||||
...
|
||||
};
|
||||
type AppParameters = {
|
||||
initialProps: $ReadOnly<{[string]: mixed, ...}>,
|
||||
rootTag: RootTag,
|
||||
fabric?: boolean,
|
||||
};
|
||||
export type Runnable = (
|
||||
appParameters: AppParameters,
|
||||
displayMode: DisplayModeType,
|
||||
) => void;
|
||||
export type Runnables = {[appKey: string]: Runnable};
|
||||
export type Registry = {
|
||||
sections: $ReadOnlyArray<string>,
|
||||
runnables: Runnables,
|
||||
...
|
||||
};
|
||||
export type WrapperComponentProvider = (
|
||||
appParameters: Object,
|
||||
) => React.ComponentType<any>;
|
||||
export type RootViewStyleProvider = (appParameters: Object) => ViewStyleProp;
|
||||
|
||||
const runnables: Runnables = {};
|
||||
let runCount = 1;
|
||||
const sections: Runnables = {};
|
||||
const taskProviders: Map<string, TaskProvider> = new Map();
|
||||
const taskCancelProviders: Map<string, TaskCancelProvider> = new Map();
|
||||
let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook =
|
||||
(component: ComponentProvider) => component();
|
||||
|
||||
let wrapperComponentProvider: ?WrapperComponentProvider;
|
||||
let rootViewStyleProvider: ?RootViewStyleProvider;
|
||||
|
||||
/**
|
||||
* `AppRegistry` is the JavaScript entry point to running all React Native apps.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry
|
||||
*/
|
||||
const AppRegistry = {
|
||||
setWrapperComponentProvider(provider: WrapperComponentProvider) {
|
||||
wrapperComponentProvider = provider;
|
||||
},
|
||||
|
||||
setRootViewStyleProvider(provider: RootViewStyleProvider) {
|
||||
rootViewStyleProvider = provider;
|
||||
},
|
||||
|
||||
registerConfig(config: Array<AppConfig>): void {
|
||||
config.forEach(appConfig => {
|
||||
if (appConfig.run) {
|
||||
AppRegistry.registerRunnable(appConfig.appKey, appConfig.run);
|
||||
} else {
|
||||
invariant(
|
||||
appConfig.component != null,
|
||||
'AppRegistry.registerConfig(...): Every config is expected to set ' +
|
||||
'either `run` or `component`, but `%s` has neither.',
|
||||
appConfig.appKey,
|
||||
);
|
||||
AppRegistry.registerComponent(
|
||||
appConfig.appKey,
|
||||
appConfig.component,
|
||||
appConfig.section,
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Registers an app's root component.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#registercomponent
|
||||
*/
|
||||
registerComponent(
|
||||
appKey: string,
|
||||
componentProvider: ComponentProvider,
|
||||
section?: boolean,
|
||||
): string {
|
||||
const scopedPerformanceLogger = createPerformanceLogger();
|
||||
runnables[appKey] = (appParameters, displayMode) => {
|
||||
renderApplication(
|
||||
componentProviderInstrumentationHook(
|
||||
componentProvider,
|
||||
scopedPerformanceLogger,
|
||||
),
|
||||
appParameters.initialProps,
|
||||
appParameters.rootTag,
|
||||
wrapperComponentProvider && wrapperComponentProvider(appParameters),
|
||||
rootViewStyleProvider && rootViewStyleProvider(appParameters),
|
||||
appParameters.fabric,
|
||||
scopedPerformanceLogger,
|
||||
appKey === 'LogBox', // is logbox
|
||||
appKey,
|
||||
displayMode,
|
||||
);
|
||||
};
|
||||
if (section) {
|
||||
sections[appKey] = runnables[appKey];
|
||||
}
|
||||
return appKey;
|
||||
},
|
||||
|
||||
registerRunnable(appKey: string, run: Runnable): string {
|
||||
runnables[appKey] = run;
|
||||
return appKey;
|
||||
},
|
||||
|
||||
registerSection(appKey: string, component: ComponentProvider): void {
|
||||
AppRegistry.registerComponent(appKey, component, true);
|
||||
},
|
||||
|
||||
getAppKeys(): $ReadOnlyArray<string> {
|
||||
return Object.keys(runnables);
|
||||
},
|
||||
|
||||
getSectionKeys(): $ReadOnlyArray<string> {
|
||||
return Object.keys(sections);
|
||||
},
|
||||
|
||||
getSections(): Runnables {
|
||||
return {
|
||||
...sections,
|
||||
};
|
||||
},
|
||||
|
||||
getRunnable(appKey: string): ?Runnable {
|
||||
return runnables[appKey];
|
||||
},
|
||||
|
||||
getRegistry(): Registry {
|
||||
return {
|
||||
sections: AppRegistry.getSectionKeys(),
|
||||
runnables: {...runnables},
|
||||
};
|
||||
},
|
||||
|
||||
setComponentProviderInstrumentationHook(
|
||||
hook: ComponentProviderInstrumentationHook,
|
||||
) {
|
||||
componentProviderInstrumentationHook = hook;
|
||||
},
|
||||
|
||||
/**
|
||||
* Loads the JavaScript bundle and runs the app.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#runapplication
|
||||
*/
|
||||
runApplication(
|
||||
appKey: string,
|
||||
appParameters: AppParameters,
|
||||
displayMode?: number,
|
||||
): void {
|
||||
if (appKey !== 'LogBox') {
|
||||
const logParams = __DEV__ ? ` with ${JSON.stringify(appParameters)}` : '';
|
||||
const msg = `Running "${appKey}"${logParams}`;
|
||||
infoLog(msg);
|
||||
BugReporting.addSource(
|
||||
'AppRegistry.runApplication' + runCount++,
|
||||
() => msg,
|
||||
);
|
||||
}
|
||||
invariant(
|
||||
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](appParameters, coerceDisplayMode(displayMode));
|
||||
},
|
||||
|
||||
/**
|
||||
* Update initial props for a surface that's already rendered
|
||||
*/
|
||||
setSurfaceProps(
|
||||
appKey: string,
|
||||
appParameters: Object,
|
||||
displayMode?: number,
|
||||
): void {
|
||||
if (appKey !== 'LogBox') {
|
||||
const msg =
|
||||
'Updating props for Surface "' +
|
||||
appKey +
|
||||
'" with ' +
|
||||
JSON.stringify(appParameters);
|
||||
infoLog(msg);
|
||||
BugReporting.addSource(
|
||||
'AppRegistry.setSurfaceProps' + runCount++,
|
||||
() => msg,
|
||||
);
|
||||
}
|
||||
invariant(
|
||||
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](appParameters, coerceDisplayMode(displayMode));
|
||||
},
|
||||
|
||||
/**
|
||||
* Stops an application when a view should be destroyed.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#unmountapplicationcomponentatroottag
|
||||
*/
|
||||
unmountApplicationComponentAtRootTag(rootTag: RootTag): void {
|
||||
unmountComponentAtNodeAndRemoveContainer(rootTag);
|
||||
},
|
||||
|
||||
/**
|
||||
* Register a headless task. A headless task is a bit of code that runs without a UI.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#registerheadlesstask
|
||||
*/
|
||||
registerHeadlessTask(taskKey: string, taskProvider: TaskProvider): void {
|
||||
// $FlowFixMe[object-this-reference]
|
||||
this.registerCancellableHeadlessTask(taskKey, taskProvider, () => () => {
|
||||
/* Cancel is no-op */
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Register a cancellable headless task. A headless task is a bit of code that runs without a UI.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#registercancellableheadlesstask
|
||||
*/
|
||||
registerCancellableHeadlessTask(
|
||||
taskKey: string,
|
||||
taskProvider: TaskProvider,
|
||||
taskCancelProvider: TaskCancelProvider,
|
||||
): void {
|
||||
if (taskProviders.has(taskKey)) {
|
||||
console.warn(
|
||||
`registerHeadlessTask or registerCancellableHeadlessTask called multiple times for same key '${taskKey}'`,
|
||||
);
|
||||
}
|
||||
taskProviders.set(taskKey, taskProvider);
|
||||
taskCancelProviders.set(taskKey, taskCancelProvider);
|
||||
},
|
||||
|
||||
/**
|
||||
* Only called from native code. Starts a headless task.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#startheadlesstask
|
||||
*/
|
||||
startHeadlessTask(taskId: number, taskKey: string, data: any): void {
|
||||
const taskProvider = taskProviders.get(taskKey);
|
||||
if (!taskProvider) {
|
||||
console.warn(`No task registered for key ${taskKey}`);
|
||||
if (NativeHeadlessJsTaskSupport) {
|
||||
NativeHeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
taskProvider()(data)
|
||||
.then(() => {
|
||||
if (NativeHeadlessJsTaskSupport) {
|
||||
NativeHeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
||||
}
|
||||
})
|
||||
.catch(reason => {
|
||||
console.error(reason);
|
||||
|
||||
if (
|
||||
NativeHeadlessJsTaskSupport &&
|
||||
reason instanceof HeadlessJsTaskError
|
||||
) {
|
||||
// $FlowFixMe[unused-promise]
|
||||
NativeHeadlessJsTaskSupport.notifyTaskRetry(taskId).then(
|
||||
retryPosted => {
|
||||
if (!retryPosted) {
|
||||
NativeHeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Only called from native code. Cancels a headless task.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#cancelheadlesstask
|
||||
*/
|
||||
cancelHeadlessTask(taskId: number, taskKey: string): void {
|
||||
const taskCancelProvider = taskCancelProviders.get(taskKey);
|
||||
if (!taskCancelProvider) {
|
||||
throw new Error(`No task canceller registered for key '${taskKey}'`);
|
||||
}
|
||||
taskCancelProvider()();
|
||||
},
|
||||
};
|
||||
import * as AppRegistry from './AppRegistryImpl';
|
||||
|
||||
// Register LogBox as a default surface
|
||||
AppRegistry.registerComponent('LogBox', () => {
|
||||
@@ -358,3 +38,4 @@ global.RN$SurfaceRegistry = {
|
||||
registerCallableModule('AppRegistry', AppRegistry);
|
||||
|
||||
export default AppRegistry;
|
||||
export {AppRegistry};
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 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
|
||||
* @format
|
||||
*/
|
||||
|
||||
export type {
|
||||
TaskProvider,
|
||||
ComponentProvider,
|
||||
ComponentProviderInstrumentationHook,
|
||||
AppConfig,
|
||||
Runnable,
|
||||
Runnables,
|
||||
Registry,
|
||||
WrapperComponentProvider,
|
||||
RootViewStyleProvider,
|
||||
} from './AppRegistry.flow';
|
||||
|
||||
export * as default from './AppRegistryImpl';
|
||||
|
||||
// This export is only for the purpose of generated TS definitions being correct.
|
||||
export * as AppRegistry from './AppRegistryImpl';
|
||||
@@ -0,0 +1,316 @@
|
||||
/**
|
||||
* 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
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {RootTag} from '../Types/RootTagTypes';
|
||||
import type {
|
||||
AppConfig,
|
||||
AppParameters,
|
||||
ComponentProvider,
|
||||
ComponentProviderInstrumentationHook,
|
||||
Registry,
|
||||
RootViewStyleProvider,
|
||||
Runnable,
|
||||
Runnables,
|
||||
TaskProvider,
|
||||
WrapperComponentProvider,
|
||||
} from './AppRegistry.flow';
|
||||
|
||||
import BugReporting from '../BugReporting/BugReporting';
|
||||
import createPerformanceLogger from '../Utilities/createPerformanceLogger';
|
||||
import infoLog from '../Utilities/infoLog';
|
||||
import SceneTracker from '../Utilities/SceneTracker';
|
||||
import {coerceDisplayMode} from './DisplayMode';
|
||||
import HeadlessJsTaskError from './HeadlessJsTaskError';
|
||||
import NativeHeadlessJsTaskSupport from './NativeHeadlessJsTaskSupport';
|
||||
import renderApplication from './renderApplication';
|
||||
import {unmountComponentAtNodeAndRemoveContainer} from './RendererProxy';
|
||||
import invariant from 'invariant';
|
||||
|
||||
type TaskCanceller = () => void;
|
||||
type TaskCancelProvider = () => TaskCanceller;
|
||||
|
||||
const runnables: Runnables = {};
|
||||
let runCount = 1;
|
||||
const sections: Runnables = {};
|
||||
const taskProviders: Map<string, TaskProvider> = new Map();
|
||||
const taskCancelProviders: Map<string, TaskCancelProvider> = new Map();
|
||||
let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook =
|
||||
(component: ComponentProvider) => component();
|
||||
|
||||
let wrapperComponentProvider: ?WrapperComponentProvider;
|
||||
let rootViewStyleProvider: ?RootViewStyleProvider;
|
||||
|
||||
export function setWrapperComponentProvider(
|
||||
provider: WrapperComponentProvider,
|
||||
) {
|
||||
wrapperComponentProvider = provider;
|
||||
}
|
||||
|
||||
export function setRootViewStyleProvider(provider: RootViewStyleProvider) {
|
||||
rootViewStyleProvider = provider;
|
||||
}
|
||||
|
||||
export function registerConfig(config: Array<AppConfig>): void {
|
||||
config.forEach(appConfig => {
|
||||
if (appConfig.run) {
|
||||
registerRunnable(appConfig.appKey, appConfig.run);
|
||||
} else {
|
||||
invariant(
|
||||
appConfig.component != null,
|
||||
'AppRegistry.registerConfig(...): Every config is expected to set ' +
|
||||
'either `run` or `component`, but `%s` has neither.',
|
||||
appConfig.appKey,
|
||||
);
|
||||
registerComponent(
|
||||
appConfig.appKey,
|
||||
appConfig.component,
|
||||
appConfig.section,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an app's root component.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#registercomponent
|
||||
*/
|
||||
export function registerComponent(
|
||||
appKey: string,
|
||||
componentProvider: ComponentProvider,
|
||||
section?: boolean,
|
||||
): string {
|
||||
const scopedPerformanceLogger = createPerformanceLogger();
|
||||
runnables[appKey] = (appParameters, displayMode) => {
|
||||
renderApplication(
|
||||
componentProviderInstrumentationHook(
|
||||
componentProvider,
|
||||
scopedPerformanceLogger,
|
||||
),
|
||||
appParameters.initialProps,
|
||||
appParameters.rootTag,
|
||||
wrapperComponentProvider && wrapperComponentProvider(appParameters),
|
||||
rootViewStyleProvider && rootViewStyleProvider(appParameters),
|
||||
appParameters.fabric,
|
||||
scopedPerformanceLogger,
|
||||
appKey === 'LogBox', // is logbox
|
||||
appKey,
|
||||
displayMode,
|
||||
);
|
||||
};
|
||||
if (section) {
|
||||
sections[appKey] = runnables[appKey];
|
||||
}
|
||||
return appKey;
|
||||
}
|
||||
|
||||
export function registerRunnable(appKey: string, run: Runnable): string {
|
||||
runnables[appKey] = run;
|
||||
return appKey;
|
||||
}
|
||||
|
||||
export function registerSection(
|
||||
appKey: string,
|
||||
component: ComponentProvider,
|
||||
): void {
|
||||
registerComponent(appKey, component, true);
|
||||
}
|
||||
|
||||
export function getAppKeys(): $ReadOnlyArray<string> {
|
||||
return Object.keys(runnables);
|
||||
}
|
||||
|
||||
export function getSectionKeys(): $ReadOnlyArray<string> {
|
||||
return Object.keys(sections);
|
||||
}
|
||||
|
||||
export function getSections(): Runnables {
|
||||
return {
|
||||
...sections,
|
||||
};
|
||||
}
|
||||
|
||||
export function getRunnable(appKey: string): ?Runnable {
|
||||
return runnables[appKey];
|
||||
}
|
||||
|
||||
export function getRegistry(): Registry {
|
||||
return {
|
||||
sections: getSectionKeys(),
|
||||
runnables: {...runnables},
|
||||
};
|
||||
}
|
||||
|
||||
export function setComponentProviderInstrumentationHook(
|
||||
hook: ComponentProviderInstrumentationHook,
|
||||
) {
|
||||
componentProviderInstrumentationHook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the JavaScript bundle and runs the app.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#runapplication
|
||||
*/
|
||||
export function runApplication(
|
||||
appKey: string,
|
||||
appParameters: AppParameters,
|
||||
displayMode?: number,
|
||||
): void {
|
||||
if (appKey !== 'LogBox') {
|
||||
const logParams = __DEV__ ? ` with ${JSON.stringify(appParameters)}` : '';
|
||||
const msg = `Running "${appKey}"${logParams}`;
|
||||
infoLog(msg);
|
||||
BugReporting.addSource(
|
||||
'AppRegistry.runApplication' + runCount++,
|
||||
() => msg,
|
||||
);
|
||||
}
|
||||
invariant(
|
||||
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](appParameters, coerceDisplayMode(displayMode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update initial props for a surface that's already rendered
|
||||
*/
|
||||
export function setSurfaceProps(
|
||||
appKey: string,
|
||||
appParameters: Object,
|
||||
displayMode?: number,
|
||||
): void {
|
||||
if (appKey !== 'LogBox') {
|
||||
const msg =
|
||||
'Updating props for Surface "' +
|
||||
appKey +
|
||||
'" with ' +
|
||||
JSON.stringify(appParameters);
|
||||
infoLog(msg);
|
||||
BugReporting.addSource(
|
||||
'AppRegistry.setSurfaceProps' + runCount++,
|
||||
() => msg,
|
||||
);
|
||||
}
|
||||
invariant(
|
||||
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](appParameters, coerceDisplayMode(displayMode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops an application when a view should be destroyed.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#unmountapplicationcomponentatroottag
|
||||
*/
|
||||
export function unmountApplicationComponentAtRootTag(rootTag: RootTag): void {
|
||||
unmountComponentAtNodeAndRemoveContainer(rootTag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a headless task. A headless task is a bit of code that runs without a UI.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#registerheadlesstask
|
||||
*/
|
||||
export function registerHeadlessTask(
|
||||
taskKey: string,
|
||||
taskProvider: TaskProvider,
|
||||
): void {
|
||||
// $FlowFixMe[object-this-reference]
|
||||
registerCancellableHeadlessTask(taskKey, taskProvider, () => () => {
|
||||
/* Cancel is no-op */
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a cancellable headless task. A headless task is a bit of code that runs without a UI.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#registercancellableheadlesstask
|
||||
*/
|
||||
export function registerCancellableHeadlessTask(
|
||||
taskKey: string,
|
||||
taskProvider: TaskProvider,
|
||||
taskCancelProvider: TaskCancelProvider,
|
||||
): void {
|
||||
if (taskProviders.has(taskKey)) {
|
||||
console.warn(
|
||||
`registerHeadlessTask or registerCancellableHeadlessTask called multiple times for same key '${taskKey}'`,
|
||||
);
|
||||
}
|
||||
taskProviders.set(taskKey, taskProvider);
|
||||
taskCancelProviders.set(taskKey, taskCancelProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only called from native code. Starts a headless task.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#startheadlesstask
|
||||
*/
|
||||
export function startHeadlessTask(
|
||||
taskId: number,
|
||||
taskKey: string,
|
||||
data: any,
|
||||
): void {
|
||||
const taskProvider = taskProviders.get(taskKey);
|
||||
if (!taskProvider) {
|
||||
console.warn(`No task registered for key ${taskKey}`);
|
||||
if (NativeHeadlessJsTaskSupport) {
|
||||
NativeHeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
taskProvider()(data)
|
||||
.then(() => {
|
||||
if (NativeHeadlessJsTaskSupport) {
|
||||
NativeHeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
||||
}
|
||||
})
|
||||
.catch(reason => {
|
||||
console.error(reason);
|
||||
|
||||
if (
|
||||
NativeHeadlessJsTaskSupport &&
|
||||
reason instanceof HeadlessJsTaskError
|
||||
) {
|
||||
// $FlowFixMe[unused-promise]
|
||||
NativeHeadlessJsTaskSupport.notifyTaskRetry(taskId).then(
|
||||
retryPosted => {
|
||||
if (!retryPosted) {
|
||||
NativeHeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Only called from native code. Cancels a headless task.
|
||||
*
|
||||
* See https://reactnative.dev/docs/appregistry#cancelheadlesstask
|
||||
*/
|
||||
export function cancelHeadlessTask(taskId: number, taskKey: string): void {
|
||||
const taskCancelProvider = taskCancelProviders.get(taskKey);
|
||||
if (!taskCancelProvider) {
|
||||
throw new Error(`No task canceller registered for key '${taskKey}'`);
|
||||
}
|
||||
taskCancelProvider()();
|
||||
}
|
||||
@@ -6814,11 +6814,9 @@ declare export default typeof AppContainer;
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`public API should not change unintentionally Libraries/ReactNative/AppRegistry.js 1`] = `
|
||||
exports[`public API should not change unintentionally Libraries/ReactNative/AppRegistry.flow.js 1`] = `
|
||||
"type Task = (taskData: any) => Promise<void>;
|
||||
export type TaskProvider = () => Task;
|
||||
type TaskCanceller = () => void;
|
||||
type TaskCancelProvider = () => TaskCanceller;
|
||||
export type ComponentProvider = () => React.ComponentType<any>;
|
||||
export type ComponentProviderInstrumentationHook = (
|
||||
component_: ComponentProvider,
|
||||
@@ -6831,7 +6829,7 @@ export type AppConfig = {
|
||||
section?: boolean,
|
||||
...
|
||||
};
|
||||
type AppParameters = {
|
||||
export type AppParameters = {
|
||||
initialProps: $ReadOnly<{ [string]: mixed, ... }>,
|
||||
rootTag: RootTag,
|
||||
fabric?: boolean,
|
||||
@@ -6850,46 +6848,91 @@ export type WrapperComponentProvider = (
|
||||
appParameters: Object
|
||||
) => React.ComponentType<any>;
|
||||
export type RootViewStyleProvider = (appParameters: Object) => ViewStyleProp;
|
||||
declare const AppRegistry: {
|
||||
setWrapperComponentProvider(provider: WrapperComponentProvider): void,
|
||||
setRootViewStyleProvider(provider: RootViewStyleProvider): void,
|
||||
registerConfig(config: Array<AppConfig>): void,
|
||||
registerComponent(
|
||||
appKey: string,
|
||||
componentProvider: ComponentProvider,
|
||||
section?: boolean
|
||||
): string,
|
||||
registerRunnable(appKey: string, run: Runnable): string,
|
||||
registerSection(appKey: string, component: ComponentProvider): void,
|
||||
getAppKeys(): $ReadOnlyArray<string>,
|
||||
getSectionKeys(): $ReadOnlyArray<string>,
|
||||
getSections(): Runnables,
|
||||
getRunnable(appKey: string): ?Runnable,
|
||||
getRegistry(): Registry,
|
||||
setComponentProviderInstrumentationHook(
|
||||
hook: ComponentProviderInstrumentationHook
|
||||
): void,
|
||||
runApplication(
|
||||
appKey: string,
|
||||
appParameters: AppParameters,
|
||||
displayMode?: number
|
||||
): void,
|
||||
setSurfaceProps(
|
||||
appKey: string,
|
||||
appParameters: Object,
|
||||
displayMode?: number
|
||||
): void,
|
||||
unmountApplicationComponentAtRootTag(rootTag: RootTag): void,
|
||||
registerHeadlessTask(taskKey: string, taskProvider: TaskProvider): void,
|
||||
registerCancellableHeadlessTask(
|
||||
taskKey: string,
|
||||
taskProvider: TaskProvider,
|
||||
taskCancelProvider: TaskCancelProvider
|
||||
): void,
|
||||
startHeadlessTask(taskId: number, taskKey: string, data: any): void,
|
||||
cancelHeadlessTask(taskId: number, taskKey: string): void,
|
||||
};
|
||||
declare export default typeof AppRegistry;
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`public API should not change unintentionally Libraries/ReactNative/AppRegistry.js 1`] = `
|
||||
"declare export default typeof AppRegistry;
|
||||
export { AppRegistry };
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`public API should not change unintentionally Libraries/ReactNative/AppRegistry.js.flow 1`] = `
|
||||
"export type {
|
||||
TaskProvider,
|
||||
ComponentProvider,
|
||||
ComponentProviderInstrumentationHook,
|
||||
AppConfig,
|
||||
Runnable,
|
||||
Runnables,
|
||||
Registry,
|
||||
WrapperComponentProvider,
|
||||
RootViewStyleProvider,
|
||||
} from \\"./AppRegistry.flow\\";
|
||||
export * as default from \\"./AppRegistryImpl\\";
|
||||
export * as AppRegistry from \\"./AppRegistryImpl\\";
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`public API should not change unintentionally Libraries/ReactNative/AppRegistryImpl.js 1`] = `
|
||||
"type TaskCanceller = () => void;
|
||||
type TaskCancelProvider = () => TaskCanceller;
|
||||
declare export function setWrapperComponentProvider(
|
||||
provider: WrapperComponentProvider
|
||||
): void;
|
||||
declare export function setRootViewStyleProvider(
|
||||
provider: RootViewStyleProvider
|
||||
): void;
|
||||
declare export function registerConfig(config: Array<AppConfig>): void;
|
||||
declare export function registerComponent(
|
||||
appKey: string,
|
||||
componentProvider: ComponentProvider,
|
||||
section?: boolean
|
||||
): string;
|
||||
declare export function registerRunnable(appKey: string, run: Runnable): string;
|
||||
declare export function registerSection(
|
||||
appKey: string,
|
||||
component: ComponentProvider
|
||||
): void;
|
||||
declare export function getAppKeys(): $ReadOnlyArray<string>;
|
||||
declare export function getSectionKeys(): $ReadOnlyArray<string>;
|
||||
declare export function getSections(): Runnables;
|
||||
declare export function getRunnable(appKey: string): ?Runnable;
|
||||
declare export function getRegistry(): Registry;
|
||||
declare export function setComponentProviderInstrumentationHook(
|
||||
hook: ComponentProviderInstrumentationHook
|
||||
): void;
|
||||
declare export function runApplication(
|
||||
appKey: string,
|
||||
appParameters: AppParameters,
|
||||
displayMode?: number
|
||||
): void;
|
||||
declare export function setSurfaceProps(
|
||||
appKey: string,
|
||||
appParameters: Object,
|
||||
displayMode?: number
|
||||
): void;
|
||||
declare export function unmountApplicationComponentAtRootTag(
|
||||
rootTag: RootTag
|
||||
): void;
|
||||
declare export function registerHeadlessTask(
|
||||
taskKey: string,
|
||||
taskProvider: TaskProvider
|
||||
): void;
|
||||
declare export function registerCancellableHeadlessTask(
|
||||
taskKey: string,
|
||||
taskProvider: TaskProvider,
|
||||
taskCancelProvider: TaskCancelProvider
|
||||
): void;
|
||||
declare export function startHeadlessTask(
|
||||
taskId: number,
|
||||
taskKey: string,
|
||||
data: any
|
||||
): void;
|
||||
declare export function cancelHeadlessTask(
|
||||
taskId: number,
|
||||
taskKey: string
|
||||
): void;
|
||||
"
|
||||
`;
|
||||
|
||||
@@ -9284,7 +9327,7 @@ export { default as ActionSheetIOS } from \\"./Libraries/ActionSheetIOS/ActionSh
|
||||
export { default as Alert } from \\"./Libraries/Alert/Alert\\";
|
||||
export { default as Animated } from \\"./Libraries/Animated/Animated\\";
|
||||
export * as Appearance from \\"./Libraries/Utilities/Appearance\\";
|
||||
export { default as AppRegistry } from \\"./Libraries/ReactNative/AppRegistry\\";
|
||||
export { AppRegistry } from \\"./Libraries/ReactNative/AppRegistry\\";
|
||||
export { default as AppState } from \\"./Libraries/AppState/AppState\\";
|
||||
export { default as BackHandler } from \\"./Libraries/Utilities/BackHandler\\";
|
||||
export { default as Clipboard } from \\"./Libraries/Components/Clipboard/Clipboard\\";
|
||||
|
||||
@@ -62,7 +62,7 @@ export {default as ActionSheetIOS} from './Libraries/ActionSheetIOS/ActionSheetI
|
||||
export {default as Alert} from './Libraries/Alert/Alert';
|
||||
export {default as Animated} from './Libraries/Animated/Animated';
|
||||
export * as Appearance from './Libraries/Utilities/Appearance';
|
||||
export {default as AppRegistry} from './Libraries/ReactNative/AppRegistry';
|
||||
export {AppRegistry} from './Libraries/ReactNative/AppRegistry';
|
||||
export {default as AppState} from './Libraries/AppState/AppState';
|
||||
export {default as BackHandler} from './Libraries/Utilities/BackHandler';
|
||||
export {default as Clipboard} from './Libraries/Components/Clipboard/Clipboard';
|
||||
|
||||
Reference in new issue
Block a user