2015-01-30 04:10:49 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-24 01:07:33 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-01-30 04:10:49 +03:00
|
|
|
*
|
2015-03-25 05:34:12 +03:00
|
|
|
* @flow
|
2017-09-25 08:57:35 +03:00
|
|
|
* @format
|
2015-01-30 04:10:49 +03:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-10 23:38:50 +03:00
|
|
|
const BatchedBridge = require('BatchedBridge');
|
|
|
|
const BugReporting = require('BugReporting');
|
2017-02-18 03:39:50 +03:00
|
|
|
const NativeModules = require('NativeModules');
|
2017-02-10 23:38:50 +03:00
|
|
|
const ReactNative = require('ReactNative');
|
2017-04-07 10:48:49 +03:00
|
|
|
const SceneTracker = require('SceneTracker');
|
2015-12-09 02:57:34 +03:00
|
|
|
|
2016-09-29 13:43:59 +03:00
|
|
|
const infoLog = require('infoLog');
|
2018-12-03 10:49:12 +03:00
|
|
|
const invariant = require('invariant');
|
2017-02-10 23:38:50 +03:00
|
|
|
const renderApplication = require('renderApplication');
|
2019-02-28 14:35:59 +03:00
|
|
|
const createPerformanceLogger = require('createPerformanceLogger');
|
|
|
|
import type {IPerformanceLogger} from 'createPerformanceLogger';
|
2016-09-29 13:43:59 +03:00
|
|
|
|
|
|
|
type Task = (taskData: any) => Promise<void>;
|
|
|
|
type TaskProvider = () => Task;
|
2019-02-15 19:23:40 +03:00
|
|
|
type TaskCanceller = () => void;
|
|
|
|
type TaskCancelProvider = () => TaskCanceller;
|
|
|
|
|
2017-08-18 04:36:54 +03:00
|
|
|
export type ComponentProvider = () => React$ComponentType<any>;
|
2017-09-25 08:57:35 +03:00
|
|
|
export type ComponentProviderInstrumentationHook = (
|
|
|
|
component: ComponentProvider,
|
2019-02-28 14:35:59 +03:00
|
|
|
scopedPerformanceLogger: IPerformanceLogger,
|
2017-09-25 08:57:35 +03:00
|
|
|
) => React$ComponentType<any>;
|
2017-02-15 13:15:22 +03:00
|
|
|
export type AppConfig = {
|
2016-08-09 16:32:41 +03:00
|
|
|
appKey: string,
|
|
|
|
component?: ComponentProvider,
|
|
|
|
run?: Function,
|
2017-02-10 23:38:50 +03:00
|
|
|
section?: boolean,
|
|
|
|
};
|
2017-02-15 13:15:22 +03:00
|
|
|
export type Runnable = {
|
2017-02-10 23:38:50 +03:00
|
|
|
component?: ComponentProvider,
|
|
|
|
run: Function,
|
|
|
|
};
|
2017-02-15 13:15:22 +03:00
|
|
|
export type Runnables = {
|
2017-02-10 23:38:50 +03:00
|
|
|
[appKey: string]: Runnable,
|
2015-03-25 05:34:12 +03:00
|
|
|
};
|
2017-02-15 13:15:22 +03:00
|
|
|
export type Registry = {
|
2017-02-15 02:21:42 +03:00
|
|
|
sections: Array<string>,
|
|
|
|
runnables: Runnables,
|
|
|
|
};
|
2017-08-18 04:36:54 +03:00
|
|
|
export type WrapperComponentProvider = any => React$ComponentType<*>;
|
2015-03-25 05:34:12 +03:00
|
|
|
|
2017-02-10 23:38:50 +03:00
|
|
|
const runnables: Runnables = {};
|
|
|
|
let runCount = 1;
|
|
|
|
const sections: Runnables = {};
|
2019-02-15 19:23:40 +03:00
|
|
|
const taskProviders: Map<string, TaskProvider> = new Map();
|
|
|
|
const taskCancelProviders: Map<string, TaskCancelProvider> = new Map();
|
2017-09-25 08:57:35 +03:00
|
|
|
let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook = (
|
|
|
|
component: ComponentProvider,
|
|
|
|
) => component();
|
2017-02-10 23:38:50 +03:00
|
|
|
|
2017-06-27 02:20:39 +03:00
|
|
|
let wrapperComponentProvider: ?WrapperComponentProvider;
|
2019-04-10 23:54:00 +03:00
|
|
|
let showFabricIndicator = false;
|
2017-06-27 02:20:39 +03:00
|
|
|
|
2015-02-19 17:57:05 +03:00
|
|
|
/**
|
2017-11-21 00:07:10 +03:00
|
|
|
* `AppRegistry` is the JavaScript entry point to running all React Native apps.
|
2017-12-07 04:25:06 +03:00
|
|
|
*
|
2017-11-21 00:07:10 +03:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html
|
2015-02-19 17:57:05 +03:00
|
|
|
*/
|
2017-02-10 23:38:50 +03:00
|
|
|
const AppRegistry = {
|
2017-06-27 02:20:39 +03:00
|
|
|
setWrapperComponentProvider(provider: WrapperComponentProvider) {
|
|
|
|
wrapperComponentProvider = provider;
|
|
|
|
},
|
|
|
|
|
2019-04-10 23:54:00 +03:00
|
|
|
enableFabricIndicator(enabled: boolean): void {
|
|
|
|
showFabricIndicator = enabled;
|
|
|
|
},
|
|
|
|
|
2017-02-10 23:38:50 +03:00
|
|
|
registerConfig(config: Array<AppConfig>): void {
|
2017-09-25 08:57:35 +03:00
|
|
|
config.forEach(appConfig => {
|
2015-03-25 05:34:12 +03:00
|
|
|
if (appConfig.run) {
|
|
|
|
AppRegistry.registerRunnable(appConfig.appKey, appConfig.run);
|
2015-01-30 04:10:49 +03:00
|
|
|
} else {
|
2017-02-10 23:38:50 +03:00
|
|
|
invariant(
|
|
|
|
appConfig.component != null,
|
|
|
|
'AppRegistry.registerConfig(...): Every config is expected to set ' +
|
2017-09-25 08:57:35 +03:00
|
|
|
'either `run` or `component`, but `%s` has neither.',
|
|
|
|
appConfig.appKey,
|
2017-02-10 23:38:50 +03:00
|
|
|
);
|
|
|
|
AppRegistry.registerComponent(
|
|
|
|
appConfig.appKey,
|
|
|
|
appConfig.component,
|
|
|
|
appConfig.section,
|
|
|
|
);
|
2015-01-30 04:10:49 +03:00
|
|
|
}
|
2017-02-10 23:38:50 +03:00
|
|
|
});
|
2015-02-19 17:57:05 +03:00
|
|
|
},
|
2015-01-30 04:10:49 +03:00
|
|
|
|
2017-11-21 00:07:10 +03:00
|
|
|
/**
|
|
|
|
* Registers an app's root component.
|
2017-12-07 04:25:06 +03:00
|
|
|
*
|
2017-11-21 00:07:10 +03:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#registercomponent
|
|
|
|
*/
|
2017-02-10 23:38:50 +03:00
|
|
|
registerComponent(
|
|
|
|
appKey: string,
|
2017-05-04 03:58:59 +03:00
|
|
|
componentProvider: ComponentProvider,
|
2017-02-10 23:38:50 +03:00
|
|
|
section?: boolean,
|
|
|
|
): string {
|
2019-02-28 14:35:59 +03:00
|
|
|
let scopedPerformanceLogger = createPerformanceLogger();
|
2015-01-30 04:10:49 +03:00
|
|
|
runnables[appKey] = {
|
2017-05-04 03:58:59 +03:00
|
|
|
componentProvider,
|
2018-03-15 02:01:32 +03:00
|
|
|
run: appParameters => {
|
2018-04-19 21:03:50 +03:00
|
|
|
renderApplication(
|
2019-02-28 14:35:59 +03:00
|
|
|
componentProviderInstrumentationHook(
|
|
|
|
componentProvider,
|
|
|
|
scopedPerformanceLogger,
|
|
|
|
),
|
2017-03-22 17:54:19 +03:00
|
|
|
appParameters.initialProps,
|
2017-06-27 02:20:39 +03:00
|
|
|
appParameters.rootTag,
|
|
|
|
wrapperComponentProvider && wrapperComponentProvider(appParameters),
|
2018-04-19 21:03:50 +03:00
|
|
|
appParameters.fabric,
|
2019-04-10 23:54:00 +03:00
|
|
|
showFabricIndicator,
|
2019-02-28 14:35:59 +03:00
|
|
|
scopedPerformanceLogger,
|
2018-03-15 02:01:32 +03:00
|
|
|
);
|
|
|
|
},
|
2015-01-30 04:10:49 +03:00
|
|
|
};
|
2017-02-10 23:38:50 +03:00
|
|
|
if (section) {
|
|
|
|
sections[appKey] = runnables[appKey];
|
|
|
|
}
|
2015-01-30 04:10:49 +03:00
|
|
|
return appKey;
|
2015-02-19 17:57:05 +03:00
|
|
|
},
|
2015-01-30 04:10:49 +03:00
|
|
|
|
2017-02-10 23:38:50 +03:00
|
|
|
registerRunnable(appKey: string, run: Function): string {
|
|
|
|
runnables[appKey] = {run};
|
2015-01-30 04:10:49 +03:00
|
|
|
return appKey;
|
2015-02-19 17:57:05 +03:00
|
|
|
},
|
2015-01-30 04:10:49 +03:00
|
|
|
|
2017-02-10 23:38:50 +03:00
|
|
|
registerSection(appKey: string, component: ComponentProvider): void {
|
|
|
|
AppRegistry.registerComponent(appKey, component, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAppKeys(): Array<string> {
|
2015-08-07 21:50:23 +03:00
|
|
|
return Object.keys(runnables);
|
|
|
|
},
|
|
|
|
|
2017-02-10 23:38:50 +03:00
|
|
|
getSectionKeys(): Array<string> {
|
|
|
|
return Object.keys(sections);
|
|
|
|
},
|
|
|
|
|
|
|
|
getSections(): Runnables {
|
|
|
|
return {
|
2017-09-25 08:57:35 +03:00
|
|
|
...sections,
|
2017-02-10 23:38:50 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getRunnable(appKey: string): ?Runnable {
|
|
|
|
return runnables[appKey];
|
|
|
|
},
|
|
|
|
|
2017-02-15 02:21:42 +03:00
|
|
|
getRegistry(): Registry {
|
|
|
|
return {
|
|
|
|
sections: AppRegistry.getSectionKeys(),
|
|
|
|
runnables: {...runnables},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-09-25 08:57:35 +03:00
|
|
|
setComponentProviderInstrumentationHook(
|
|
|
|
hook: ComponentProviderInstrumentationHook,
|
|
|
|
) {
|
2017-03-22 17:54:19 +03:00
|
|
|
componentProviderInstrumentationHook = hook;
|
|
|
|
},
|
|
|
|
|
2017-11-21 00:07:10 +03:00
|
|
|
/**
|
|
|
|
* Loads the JavaScript bundle and runs the app.
|
2017-12-07 04:25:06 +03:00
|
|
|
*
|
2017-11-21 00:07:10 +03:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#runapplication
|
|
|
|
*/
|
2017-02-10 23:38:50 +03:00
|
|
|
runApplication(appKey: string, appParameters: any): void {
|
2016-04-20 18:07:43 +03:00
|
|
|
const msg =
|
2017-09-25 08:57:35 +03:00
|
|
|
'Running application "' +
|
|
|
|
appKey +
|
|
|
|
'" with appParams: ' +
|
|
|
|
JSON.stringify(appParameters) +
|
|
|
|
'. ' +
|
|
|
|
'__DEV__ === ' +
|
|
|
|
String(__DEV__) +
|
|
|
|
', development-level warning are ' +
|
|
|
|
(__DEV__ ? 'ON' : 'OFF') +
|
|
|
|
', performance optimizations are ' +
|
|
|
|
(__DEV__ ? 'OFF' : 'ON');
|
2016-05-17 22:42:17 +03:00
|
|
|
infoLog(msg);
|
2017-09-25 08:57:35 +03:00
|
|
|
BugReporting.addSource(
|
|
|
|
'AppRegistry.runApplication' + runCount++,
|
|
|
|
() => msg,
|
|
|
|
);
|
2015-01-30 04:10:49 +03:00
|
|
|
invariant(
|
|
|
|
runnables[appKey] && runnables[appKey].run,
|
2017-09-25 08:57:35 +03:00
|
|
|
'Application ' +
|
|
|
|
appKey +
|
|
|
|
' has not been registered.\n\n' +
|
|
|
|
"Hint: This error often happens when you're running the packager " +
|
|
|
|
'(local dev server) from a wrong folder. For example you have ' +
|
|
|
|
'multiple apps and the packager is still running for the app you ' +
|
|
|
|
'were working on before.\nIf this is the case, simply kill the old ' +
|
|
|
|
'packager instance (e.g. close the packager terminal window) ' +
|
|
|
|
'and start the packager in the correct app folder (e.g. cd into app ' +
|
|
|
|
"folder and run 'npm start').\n\n" +
|
|
|
|
'This error can also happen due to a require() error during ' +
|
|
|
|
'initialization or failure to call AppRegistry.registerComponent.\n\n',
|
2015-01-30 04:10:49 +03:00
|
|
|
);
|
2017-06-01 14:07:43 +03:00
|
|
|
|
2017-04-07 10:48:49 +03:00
|
|
|
SceneTracker.setActiveScene({name: appKey});
|
2015-01-30 04:10:49 +03:00
|
|
|
runnables[appKey].run(appParameters);
|
2015-02-19 17:57:05 +03:00
|
|
|
},
|
2015-12-09 02:57:34 +03:00
|
|
|
|
2017-11-21 00:07:10 +03:00
|
|
|
/**
|
2017-12-07 04:25:06 +03:00
|
|
|
* Stops an application when a view should be destroyed.
|
|
|
|
*
|
2017-11-21 00:07:10 +03:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#unmountapplicationcomponentatroottag
|
|
|
|
*/
|
2017-02-10 23:38:50 +03:00
|
|
|
unmountApplicationComponentAtRootTag(rootTag: number): void {
|
2015-12-09 02:57:34 +03:00
|
|
|
ReactNative.unmountComponentAtNodeAndRemoveContainer(rootTag);
|
|
|
|
},
|
|
|
|
|
2016-09-29 13:43:59 +03:00
|
|
|
/**
|
|
|
|
* Register a headless task. A headless task is a bit of code that runs without a UI.
|
2017-12-07 04:25:06 +03:00
|
|
|
*
|
2017-11-21 00:07:10 +03:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#registerheadlesstask
|
2016-09-29 13:43:59 +03:00
|
|
|
*/
|
2019-02-15 19:23:40 +03:00
|
|
|
registerHeadlessTask(taskKey: string, taskProvider: TaskProvider): void {
|
|
|
|
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 http://facebook.github.io/react-native/docs/appregistry.html#registercancellableheadlesstask
|
|
|
|
*/
|
|
|
|
registerCancellableHeadlessTask(
|
|
|
|
taskKey: string,
|
|
|
|
taskProvider: TaskProvider,
|
|
|
|
taskCancelProvider: TaskCancelProvider,
|
|
|
|
): void {
|
|
|
|
if (taskProviders.has(taskKey)) {
|
2017-09-25 08:57:35 +03:00
|
|
|
console.warn(
|
2019-02-15 19:23:40 +03:00
|
|
|
`registerHeadlessTask or registerCancellableHeadlessTask called multiple times for same key '${taskKey}'`,
|
2017-09-25 08:57:35 +03:00
|
|
|
);
|
2016-09-29 13:43:59 +03:00
|
|
|
}
|
2019-02-15 19:23:40 +03:00
|
|
|
taskProviders.set(taskKey, taskProvider);
|
|
|
|
taskCancelProviders.set(taskKey, taskCancelProvider);
|
2016-09-29 13:43:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only called from native code. Starts a headless task.
|
|
|
|
*
|
2017-11-21 00:07:10 +03:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#startheadlesstask
|
2016-09-29 13:43:59 +03:00
|
|
|
*/
|
2017-02-10 23:38:50 +03:00
|
|
|
startHeadlessTask(taskId: number, taskKey: string, data: any): void {
|
2019-02-15 19:23:40 +03:00
|
|
|
const taskProvider = taskProviders.get(taskKey);
|
2016-09-29 13:43:59 +03:00
|
|
|
if (!taskProvider) {
|
|
|
|
throw new Error(`No task registered for key ${taskKey}`);
|
|
|
|
}
|
|
|
|
taskProvider()(data)
|
2017-09-25 08:57:35 +03:00
|
|
|
.then(() =>
|
|
|
|
NativeModules.HeadlessJsTaskSupport.notifyTaskFinished(taskId),
|
|
|
|
)
|
2016-09-29 13:43:59 +03:00
|
|
|
.catch(reason => {
|
|
|
|
console.error(reason);
|
2017-02-18 03:39:50 +03:00
|
|
|
NativeModules.HeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
2016-09-29 13:43:59 +03:00
|
|
|
});
|
2017-09-25 08:57:35 +03:00
|
|
|
},
|
2019-02-15 19:23:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Only called from native code. Cancels a headless task.
|
|
|
|
*
|
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#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()();
|
|
|
|
},
|
2015-02-19 17:57:05 +03:00
|
|
|
};
|
2015-01-30 04:10:49 +03:00
|
|
|
|
2017-09-25 08:57:35 +03:00
|
|
|
BatchedBridge.registerCallableModule('AppRegistry', AppRegistry);
|
2015-12-09 02:57:34 +03:00
|
|
|
|
2015-02-19 17:57:05 +03:00
|
|
|
module.exports = AppRegistry;
|