2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2015-02-19 14:57:05 +00:00
|
|
|
* @providesModule AppRegistry
|
2015-03-25 02:34:12 +00:00
|
|
|
* @flow
|
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-02-15 10:15:22 +00:00
|
|
|
export type ComponentProvider = () => ReactClass<any>;
|
2017-03-22 14:54:19 +00:00
|
|
|
export type ComponentProviderInstrumentationHook =
|
|
|
|
(component: ComponentProvider) => ReactClass<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-06-26 23:20:39 +00:00
|
|
|
export type WrapperComponentProvider = any => ReactClass<*>;
|
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-03-22 14:54:19 +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-04-26 14:09:36 +00:00
|
|
|
* <div class="banner-crna-ejected">
|
|
|
|
* <h3>Project with Native Code Required</h3>
|
|
|
|
* <p>
|
|
|
|
* This API only works in projects made with <code>react-native init</code>
|
|
|
|
* or in those made with Create React Native App which have since ejected. For
|
|
|
|
* more information about ejecting, please see
|
|
|
|
* the <a href="https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md" target="_blank">guide</a> on
|
|
|
|
* the Create React Native App repository.
|
|
|
|
* </p>
|
|
|
|
* </div>
|
|
|
|
*
|
2015-02-19 14:57:05 +00:00
|
|
|
* `AppRegistry` is the JS entry point to running all React Native apps. App
|
|
|
|
* root components should register themselves with
|
|
|
|
* `AppRegistry.registerComponent`, then the native system can load the bundle
|
|
|
|
* for the app and then actually run the app when it's ready by invoking
|
|
|
|
* `AppRegistry.runApplication`.
|
|
|
|
*
|
2015-12-08 23:57:34 +00:00
|
|
|
* To "stop" an application when a view should be destroyed, call
|
|
|
|
* `AppRegistry.unmountApplicationComponentAtRootTag` with the tag that was
|
2016-12-16 18:15:07 +00:00
|
|
|
* passed into `runApplication`. These should always be used as a pair.
|
2015-12-08 23:57:34 +00:00
|
|
|
*
|
2015-02-19 14:57:05 +00:00
|
|
|
* `AppRegistry` should be `require`d early in the `require` sequence to make
|
|
|
|
* sure the JS execution environment is setup before other modules are
|
|
|
|
* `require`d.
|
|
|
|
*/
|
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 {
|
|
|
|
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 ' +
|
|
|
|
'either `run` or `component`, but `%s` has neither.',
|
|
|
|
appConfig.appKey
|
|
|
|
);
|
|
|
|
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-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,
|
2015-01-30 01:10:49 +00:00
|
|
|
run: (appParameters) =>
|
2017-03-22 14:54:19 +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),
|
2017-03-22 14:54:19 +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 {
|
|
|
|
...sections
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getRunnable(appKey: string): ?Runnable {
|
|
|
|
return runnables[appKey];
|
|
|
|
},
|
|
|
|
|
2017-02-14 23:21:42 +00:00
|
|
|
getRegistry(): Registry {
|
|
|
|
return {
|
|
|
|
sections: AppRegistry.getSectionKeys(),
|
|
|
|
runnables: {...runnables},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-03-22 14:54:19 +00:00
|
|
|
setComponentProviderInstrumentationHook(hook: ComponentProviderInstrumentationHook) {
|
|
|
|
componentProviderInstrumentationHook = hook;
|
|
|
|
},
|
|
|
|
|
2017-02-10 20:38:50 +00:00
|
|
|
runApplication(appKey: string, appParameters: any): void {
|
2016-04-20 15:07:43 +00:00
|
|
|
const msg =
|
2015-03-11 02:11:28 +00:00
|
|
|
'Running application "' + appKey + '" with appParams: ' +
|
|
|
|
JSON.stringify(appParameters) + '. ' +
|
2015-06-22 16:43:30 +00:00
|
|
|
'__DEV__ === ' + String(__DEV__) +
|
2015-03-11 02:11:28 +00:00
|
|
|
', development-level warning are ' + (__DEV__ ? 'ON' : 'OFF') +
|
2016-04-20 15:07:43 +00:00
|
|
|
', performance optimizations are ' + (__DEV__ ? 'OFF' : 'ON');
|
2016-05-17 19:42:17 +00:00
|
|
|
infoLog(msg);
|
2016-04-21 03:44:28 +00:00
|
|
|
BugReporting.addSource('AppRegistry.runApplication' + runCount++, () => msg);
|
2015-01-30 01:10:49 +00:00
|
|
|
invariant(
|
|
|
|
runnables[appKey] && runnables[appKey].run,
|
2017-02-22 22:10:32 +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-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.
|
|
|
|
* @param taskKey the key associated with this task
|
|
|
|
* @param task a promise returning function that takes some data passed from the native side as
|
|
|
|
* the only argument; when the promise is resolved or rejected the native side is
|
|
|
|
* notified of this event and it may decide to destroy the JS context.
|
|
|
|
*/
|
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)) {
|
|
|
|
console.warn(`registerHeadlessTask called multiple times for same key '${taskKey}'`);
|
|
|
|
}
|
|
|
|
tasks.set(taskKey, task);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only called from native code. Starts a headless task.
|
|
|
|
*
|
|
|
|
* @param taskId the native id for this task instance to keep track of its execution
|
|
|
|
* @param taskKey the key for the task to start
|
|
|
|
* @param data the data to pass to the task
|
|
|
|
*/
|
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-02-18 00:39:50 +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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-19 14:57:05 +00:00
|
|
|
};
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
BatchedBridge.registerCallableModule(
|
|
|
|
'AppRegistry',
|
|
|
|
AppRegistry
|
|
|
|
);
|
|
|
|
|
2015-02-19 14:57:05 +00:00
|
|
|
module.exports = AppRegistry;
|