2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00: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 01:10:49 +00:00
|
|
|
*
|
2015-03-25 02:34:12 +00:00
|
|
|
* @flow
|
2017-09-25 05:57:35 +00:00
|
|
|
* @format
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-10 20:38:50 +00:00
|
|
|
const BatchedBridge = require('BatchedBridge');
|
|
|
|
const BugReporting = require('BugReporting');
|
2017-02-18 00:39:50 +00:00
|
|
|
const NativeModules = require('NativeModules');
|
2017-02-10 20:38:50 +00:00
|
|
|
const ReactNative = require('ReactNative');
|
2017-04-07 07:48:49 +00:00
|
|
|
const SceneTracker = require('SceneTracker');
|
2015-12-08 23:57:34 +00:00
|
|
|
|
2016-09-29 10:43:59 +00:00
|
|
|
const infoLog = require('infoLog');
|
2017-02-10 20:38:50 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
|
|
|
const renderApplication = require('renderApplication');
|
2016-09-29 10:43:59 +00:00
|
|
|
|
|
|
|
type Task = (taskData: any) => Promise<void>;
|
|
|
|
type TaskProvider = () => Task;
|
2017-08-18 01:36:54 +00:00
|
|
|
export type ComponentProvider = () => React$ComponentType<any>;
|
2017-09-25 05:57:35 +00:00
|
|
|
export type ComponentProviderInstrumentationHook = (
|
|
|
|
component: ComponentProvider,
|
|
|
|
) => React$ComponentType<any>;
|
2017-02-15 10:15:22 +00:00
|
|
|
export type AppConfig = {
|
2016-08-09 13:32:41 +00:00
|
|
|
appKey: string,
|
|
|
|
component?: ComponentProvider,
|
|
|
|
run?: Function,
|
2017-02-10 20:38:50 +00:00
|
|
|
section?: boolean,
|
|
|
|
};
|
2017-02-15 10:15:22 +00:00
|
|
|
export type Runnable = {
|
2017-02-10 20:38:50 +00:00
|
|
|
component?: ComponentProvider,
|
|
|
|
run: Function,
|
|
|
|
};
|
2017-02-15 10:15:22 +00:00
|
|
|
export type Runnables = {
|
2017-02-10 20:38:50 +00:00
|
|
|
[appKey: string]: Runnable,
|
2015-03-25 02:34:12 +00:00
|
|
|
};
|
2017-02-15 10:15:22 +00:00
|
|
|
export type Registry = {
|
2017-02-14 23:21:42 +00:00
|
|
|
sections: Array<string>,
|
|
|
|
runnables: Runnables,
|
|
|
|
};
|
2017-08-18 01:36:54 +00:00
|
|
|
export type WrapperComponentProvider = any => React$ComponentType<*>;
|
2015-03-25 02:34:12 +00:00
|
|
|
|
2017-02-10 20:38:50 +00:00
|
|
|
const runnables: Runnables = {};
|
|
|
|
let runCount = 1;
|
|
|
|
const sections: Runnables = {};
|
|
|
|
const tasks: Map<string, TaskProvider> = new Map();
|
2017-09-25 05:57:35 +00:00
|
|
|
let componentProviderInstrumentationHook: ComponentProviderInstrumentationHook = (
|
|
|
|
component: ComponentProvider,
|
|
|
|
) => component();
|
2017-02-10 20:38:50 +00:00
|
|
|
|
2017-06-26 23:20:39 +00:00
|
|
|
let wrapperComponentProvider: ?WrapperComponentProvider;
|
|
|
|
|
2015-02-19 14:57:05 +00:00
|
|
|
/**
|
2017-11-20 21:07:10 +00:00
|
|
|
* `AppRegistry` is the JavaScript entry point to running all React Native apps.
|
2017-12-07 01:25:06 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html
|
2015-02-19 14:57:05 +00:00
|
|
|
*/
|
2017-02-10 20:38:50 +00:00
|
|
|
const AppRegistry = {
|
2017-06-26 23:20:39 +00:00
|
|
|
setWrapperComponentProvider(provider: WrapperComponentProvider) {
|
|
|
|
wrapperComponentProvider = provider;
|
|
|
|
},
|
|
|
|
|
2017-02-10 20:38:50 +00:00
|
|
|
registerConfig(config: Array<AppConfig>): void {
|
2017-09-25 05:57:35 +00:00
|
|
|
config.forEach(appConfig => {
|
2015-03-25 02:34:12 +00:00
|
|
|
if (appConfig.run) {
|
|
|
|
AppRegistry.registerRunnable(appConfig.appKey, appConfig.run);
|
2015-01-30 01:10:49 +00:00
|
|
|
} else {
|
2017-02-10 20:38:50 +00:00
|
|
|
invariant(
|
|
|
|
appConfig.component != null,
|
|
|
|
'AppRegistry.registerConfig(...): Every config is expected to set ' +
|
2017-09-25 05:57:35 +00:00
|
|
|
'either `run` or `component`, but `%s` has neither.',
|
|
|
|
appConfig.appKey,
|
2017-02-10 20:38:50 +00:00
|
|
|
);
|
|
|
|
AppRegistry.registerComponent(
|
|
|
|
appConfig.appKey,
|
|
|
|
appConfig.component,
|
|
|
|
appConfig.section,
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
2017-02-10 20:38:50 +00:00
|
|
|
});
|
2015-02-19 14:57:05 +00:00
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-11-20 21:07:10 +00:00
|
|
|
/**
|
|
|
|
* Registers an app's root component.
|
2017-12-07 01:25:06 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#registercomponent
|
|
|
|
*/
|
2017-02-10 20:38:50 +00:00
|
|
|
registerComponent(
|
|
|
|
appKey: string,
|
2017-05-04 00:58:59 +00:00
|
|
|
componentProvider: ComponentProvider,
|
2017-02-10 20:38:50 +00:00
|
|
|
section?: boolean,
|
|
|
|
): string {
|
2015-01-30 01:10:49 +00:00
|
|
|
runnables[appKey] = {
|
2017-05-04 00:58:59 +00:00
|
|
|
componentProvider,
|
2018-03-14 23:01:32 +00:00
|
|
|
run: appParameters => {
|
2018-04-19 18:03:50 +00:00
|
|
|
renderApplication(
|
2017-05-04 00:58:59 +00:00
|
|
|
componentProviderInstrumentationHook(componentProvider),
|
2017-03-22 14:54:19 +00:00
|
|
|
appParameters.initialProps,
|
2017-06-26 23:20:39 +00:00
|
|
|
appParameters.rootTag,
|
|
|
|
wrapperComponentProvider && wrapperComponentProvider(appParameters),
|
2018-04-19 18:03:50 +00:00
|
|
|
appParameters.fabric,
|
2018-03-14 23:01:32 +00:00
|
|
|
);
|
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
};
|
2017-02-10 20:38:50 +00:00
|
|
|
if (section) {
|
|
|
|
sections[appKey] = runnables[appKey];
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
return appKey;
|
2015-02-19 14:57:05 +00:00
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-02-10 20:38:50 +00:00
|
|
|
registerRunnable(appKey: string, run: Function): string {
|
|
|
|
runnables[appKey] = {run};
|
2015-01-30 01:10:49 +00:00
|
|
|
return appKey;
|
2015-02-19 14:57:05 +00:00
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-02-10 20:38:50 +00:00
|
|
|
registerSection(appKey: string, component: ComponentProvider): void {
|
|
|
|
AppRegistry.registerComponent(appKey, component, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAppKeys(): Array<string> {
|
2015-08-07 18:50:23 +00:00
|
|
|
return Object.keys(runnables);
|
|
|
|
},
|
|
|
|
|
2017-02-10 20:38:50 +00:00
|
|
|
getSectionKeys(): Array<string> {
|
|
|
|
return Object.keys(sections);
|
|
|
|
},
|
|
|
|
|
|
|
|
getSections(): Runnables {
|
|
|
|
return {
|
2017-09-25 05:57:35 +00:00
|
|
|
...sections,
|
2017-02-10 20:38:50 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getRunnable(appKey: string): ?Runnable {
|
|
|
|
return runnables[appKey];
|
|
|
|
},
|
|
|
|
|
2017-02-14 23:21:42 +00:00
|
|
|
getRegistry(): Registry {
|
|
|
|
return {
|
|
|
|
sections: AppRegistry.getSectionKeys(),
|
|
|
|
runnables: {...runnables},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-09-25 05:57:35 +00:00
|
|
|
setComponentProviderInstrumentationHook(
|
|
|
|
hook: ComponentProviderInstrumentationHook,
|
|
|
|
) {
|
2017-03-22 14:54:19 +00:00
|
|
|
componentProviderInstrumentationHook = hook;
|
|
|
|
},
|
|
|
|
|
2017-11-20 21:07:10 +00:00
|
|
|
/**
|
|
|
|
* Loads the JavaScript bundle and runs the app.
|
2017-12-07 01:25:06 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#runapplication
|
|
|
|
*/
|
2017-02-10 20:38:50 +00:00
|
|
|
runApplication(appKey: string, appParameters: any): void {
|
2016-04-20 15:07:43 +00:00
|
|
|
const msg =
|
2017-09-25 05:57:35 +00: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 19:42:17 +00:00
|
|
|
infoLog(msg);
|
2017-09-25 05:57:35 +00:00
|
|
|
BugReporting.addSource(
|
|
|
|
'AppRegistry.runApplication' + runCount++,
|
|
|
|
() => msg,
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
invariant(
|
|
|
|
runnables[appKey] && runnables[appKey].run,
|
2017-09-25 05:57:35 +00: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 01:10:49 +00:00
|
|
|
);
|
2017-06-01 11:07:43 +00:00
|
|
|
|
2017-04-07 07:48:49 +00:00
|
|
|
SceneTracker.setActiveScene({name: appKey});
|
2015-01-30 01:10:49 +00:00
|
|
|
runnables[appKey].run(appParameters);
|
2015-02-19 14:57:05 +00:00
|
|
|
},
|
2015-12-08 23:57:34 +00:00
|
|
|
|
2017-11-20 21:07:10 +00:00
|
|
|
/**
|
2017-12-07 01:25:06 +00:00
|
|
|
* Stops an application when a view should be destroyed.
|
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#unmountapplicationcomponentatroottag
|
|
|
|
*/
|
2017-02-10 20:38:50 +00:00
|
|
|
unmountApplicationComponentAtRootTag(rootTag: number): void {
|
2015-12-08 23:57:34 +00:00
|
|
|
ReactNative.unmountComponentAtNodeAndRemoveContainer(rootTag);
|
|
|
|
},
|
|
|
|
|
2016-09-29 10:43:59 +00:00
|
|
|
/**
|
|
|
|
* Register a headless task. A headless task is a bit of code that runs without a UI.
|
2017-12-07 01:25:06 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#registerheadlesstask
|
2016-09-29 10:43:59 +00:00
|
|
|
*/
|
2017-02-10 20:38:50 +00:00
|
|
|
registerHeadlessTask(taskKey: string, task: TaskProvider): void {
|
2016-09-29 10:43:59 +00:00
|
|
|
if (tasks.has(taskKey)) {
|
2017-09-25 05:57:35 +00:00
|
|
|
console.warn(
|
|
|
|
`registerHeadlessTask called multiple times for same key '${taskKey}'`,
|
|
|
|
);
|
2016-09-29 10:43:59 +00:00
|
|
|
}
|
|
|
|
tasks.set(taskKey, task);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only called from native code. Starts a headless task.
|
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/appregistry.html#startheadlesstask
|
2016-09-29 10:43:59 +00:00
|
|
|
*/
|
2017-02-10 20:38:50 +00:00
|
|
|
startHeadlessTask(taskId: number, taskKey: string, data: any): void {
|
2016-09-29 10:43:59 +00:00
|
|
|
const taskProvider = tasks.get(taskKey);
|
|
|
|
if (!taskProvider) {
|
|
|
|
throw new Error(`No task registered for key ${taskKey}`);
|
|
|
|
}
|
|
|
|
taskProvider()(data)
|
2017-09-25 05:57:35 +00:00
|
|
|
.then(() =>
|
|
|
|
NativeModules.HeadlessJsTaskSupport.notifyTaskFinished(taskId),
|
|
|
|
)
|
2016-09-29 10:43:59 +00:00
|
|
|
.catch(reason => {
|
|
|
|
console.error(reason);
|
2017-02-18 00:39:50 +00:00
|
|
|
NativeModules.HeadlessJsTaskSupport.notifyTaskFinished(taskId);
|
2016-09-29 10:43:59 +00:00
|
|
|
});
|
2017-09-25 05:57:35 +00:00
|
|
|
},
|
2015-02-19 14:57:05 +00:00
|
|
|
};
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-09-25 05:57:35 +00:00
|
|
|
BatchedBridge.registerCallableModule('AppRegistry', AppRegistry);
|
2015-12-08 23:57:34 +00:00
|
|
|
|
2015-02-19 14:57:05 +00:00
|
|
|
module.exports = AppRegistry;
|