You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.native.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* global __DEV__ */
  2. import React from 'react';
  3. import { Linking, Navigator, Platform } from 'react-native';
  4. import { Provider } from 'react-redux';
  5. import { _getRouteToRender } from '../functions';
  6. import { AbstractApp } from './AbstractApp';
  7. /**
  8. * Root application component.
  9. *
  10. * @extends AbstractApp
  11. */
  12. export class App extends AbstractApp {
  13. /**
  14. * App component's property types.
  15. *
  16. * @static
  17. */
  18. static propTypes = AbstractApp.propTypes
  19. /**
  20. * Initializes a new App instance.
  21. *
  22. * @param {Object} props - The read-only React Component props with which
  23. * the new instance is to be initialized.
  24. */
  25. constructor(props) {
  26. super(props);
  27. // Bind event handlers so they are only bound once for every instance.
  28. this._navigatorRenderScene = this._navigatorRenderScene.bind(this);
  29. this._onLinkingURL = this._onLinkingURL.bind(this);
  30. // In the Release configuration, React Native will (intentionally) throw
  31. // an unhandled JavascriptException for an unhandled JavaScript error.
  32. // This will effectively kill the application. In accord with the Web,
  33. // do not kill the application.
  34. this._maybeDisableExceptionsManager();
  35. }
  36. /**
  37. * Subscribe to notifications about activating URLs registered to be handled
  38. * by this app.
  39. *
  40. * @inheritdoc
  41. * @see https://facebook.github.io/react-native/docs/linking.html
  42. * @returns {void}
  43. */
  44. componentWillMount() {
  45. super.componentWillMount();
  46. Linking.addEventListener('url', this._onLinkingURL);
  47. }
  48. /**
  49. * Unsubscribe from notifications about activating URLs registered to be
  50. * handled by this app.
  51. *
  52. * @inheritdoc
  53. * @see https://facebook.github.io/react-native/docs/linking.html
  54. * @returns {void}
  55. */
  56. componentWillUnmount() {
  57. Linking.removeEventListener('url', this._onLinkingURL);
  58. super.componentWillUnmount();
  59. }
  60. /**
  61. * Implements React's {@link Component#render()}.
  62. *
  63. * @inheritdoc
  64. * @returns {ReactElement}
  65. */
  66. render() {
  67. const store = this.props.store;
  68. /* eslint-disable brace-style, react/jsx-no-bind */
  69. return (
  70. <Provider store = { store }>
  71. <Navigator
  72. initialRoute = { _getRouteToRender(store.getState) }
  73. ref = { navigator => { this.navigator = navigator; } }
  74. renderScene = { this._navigatorRenderScene } />
  75. </Provider>
  76. );
  77. /* eslint-enable brace-style, react/jsx-no-bind */
  78. }
  79. /**
  80. * Navigates to a specific Route (via platform-specific means).
  81. *
  82. * @param {Route} route - The Route to which to navigate.
  83. * @returns {void}
  84. */
  85. _navigate(route) {
  86. const navigator = this.navigator;
  87. // TODO Currently, the replace method doesn't support animation. Work
  88. // towards adding it is done in
  89. // https://github.com/facebook/react-native/issues/1981
  90. // XXX React Native's Navigator adds properties to the route it's
  91. // provided with. Clone the specified route in order to prevent its
  92. // modification.
  93. navigator && navigator.replace({ ...route });
  94. }
  95. /**
  96. * Attempts to disable the use of React Native
  97. * {@link ExceptionsManager#handleException} on platforms and in
  98. * configurations on/in which the use of the method in questions has been
  99. * determined to be undesirable. For example, React Native will
  100. * (intentionally) throw an unhandled JavascriptException for an
  101. * unhandled JavaScript error in the Release configuration. This will
  102. * effectively kill the application. In accord with the Web, do not kill the
  103. * application.
  104. *
  105. * @private
  106. * @returns {void}
  107. */
  108. _maybeDisableExceptionsManager() {
  109. if (__DEV__) {
  110. // As mentioned above, only the Release configuration was observed
  111. // to suffer.
  112. return;
  113. }
  114. if (Platform.OS !== 'android') {
  115. // A solution based on RTCSetFatalHandler was implemented on iOS and
  116. // it is preferred because it is at a later step of the
  117. // error/exception handling and it is specific to fatal
  118. // errors/exceptions which were observed to kill the application.
  119. // The solution implemented bellow was tested on Android only so it
  120. // is considered safest to use it there only.
  121. return;
  122. }
  123. const oldHandler = global.ErrorUtils.getGlobalHandler();
  124. const newHandler = _handleException;
  125. if (!oldHandler || oldHandler !== newHandler) {
  126. newHandler.next = oldHandler;
  127. global.ErrorUtils.setGlobalHandler(newHandler);
  128. }
  129. }
  130. /**
  131. * Renders the scene identified by a specific route in the Navigator of this
  132. * instance.
  133. *
  134. * @param {Object} route - The route which identifies the scene to be
  135. * rendered in the associated Navigator. In the fashion of NavigatorIOS, the
  136. * specified route is expected to define a value for its component property
  137. * which is the type of React component to be rendered.
  138. * @private
  139. * @returns {ReactElement}
  140. */
  141. _navigatorRenderScene(route) {
  142. // We started with NavigatorIOS and then switched to Navigator in order
  143. // to support Android as well. In order to reduce the number of
  144. // modifications, accept the same format of route definition.
  145. return this._createElement(route.component, {});
  146. }
  147. /**
  148. * Notified by React's Linking API that a specific URL registered to be
  149. * handled by this App was activated.
  150. *
  151. * @param {Object} event - The details of the notification/event.
  152. * @param {string} event.url - The URL registered to be handled by this App
  153. * which was activated.
  154. * @private
  155. * @returns {void}
  156. */
  157. _onLinkingURL(event) {
  158. this._openURL(event.url);
  159. }
  160. }
  161. /**
  162. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  163. * from converting a fatal error into an unhandled native exception which will
  164. * kill the application.
  165. *
  166. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  167. * @param {boolean} fatal - True if the specified error is fatal; otherwise,
  168. * false.
  169. * @private
  170. * @returns {void}
  171. */
  172. function _handleException(error, fatal) {
  173. if (fatal) {
  174. // In the Release configuration, React Native will (intentionally) throw
  175. // an unhandled JavascriptException for an unhandled JavaScript error.
  176. // This will effectively kill the application. In accord with the Web,
  177. // do not kill the application.
  178. console.error(error);
  179. } else {
  180. // Forward to the next globalHandler of ErrorUtils.
  181. const next = _handleException.next;
  182. typeof next === 'function' && next(error, fatal);
  183. }
  184. }