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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* global __DEV__ */
  2. import { Linking } from 'react-native';
  3. import { Platform } from '../../base/react';
  4. import { AbstractApp } from './AbstractApp';
  5. /**
  6. * Root application component.
  7. *
  8. * @extends AbstractApp
  9. */
  10. export class App extends AbstractApp {
  11. /**
  12. * App component's property types.
  13. *
  14. * @static
  15. */
  16. static propTypes = AbstractApp.propTypes
  17. /**
  18. * Initializes a new App instance.
  19. *
  20. * @param {Object} props - The read-only React Component props with which
  21. * the new instance is to be initialized.
  22. */
  23. constructor(props) {
  24. super(props);
  25. // Bind event handlers so they are only bound once for every instance.
  26. this._onLinkingURL = this._onLinkingURL.bind(this);
  27. // In the Release configuration, React Native will (intentionally) throw
  28. // an unhandled JavascriptException for an unhandled JavaScript error.
  29. // This will effectively kill the application. In accord with the Web,
  30. // do not kill the application.
  31. this._maybeDisableExceptionsManager();
  32. }
  33. /**
  34. * Subscribe to notifications about activating URLs registered to be handled
  35. * by this app.
  36. *
  37. * @inheritdoc
  38. * @see https://facebook.github.io/react-native/docs/linking.html
  39. * @returns {void}
  40. */
  41. componentWillMount() {
  42. super.componentWillMount();
  43. Linking.addEventListener('url', this._onLinkingURL);
  44. }
  45. /**
  46. * Unsubscribe from notifications about activating URLs registered to be
  47. * handled by this app.
  48. *
  49. * @inheritdoc
  50. * @see https://facebook.github.io/react-native/docs/linking.html
  51. * @returns {void}
  52. */
  53. componentWillUnmount() {
  54. Linking.removeEventListener('url', this._onLinkingURL);
  55. super.componentWillUnmount();
  56. }
  57. /**
  58. * Attempts to disable the use of React Native
  59. * {@link ExceptionsManager#handleException} on platforms and in
  60. * configurations on/in which the use of the method in questions has been
  61. * determined to be undesirable. For example, React Native will
  62. * (intentionally) throw an unhandled JavascriptException for an
  63. * unhandled JavaScript error in the Release configuration. This will
  64. * effectively kill the application. In accord with the Web, do not kill the
  65. * application.
  66. *
  67. * @private
  68. * @returns {void}
  69. */
  70. _maybeDisableExceptionsManager() {
  71. if (__DEV__) {
  72. // As mentioned above, only the Release configuration was observed
  73. // to suffer.
  74. return;
  75. }
  76. if (Platform.OS !== 'android') {
  77. // A solution based on RTCSetFatalHandler was implemented on iOS and
  78. // it is preferred because it is at a later step of the
  79. // error/exception handling and it is specific to fatal
  80. // errors/exceptions which were observed to kill the application.
  81. // The solution implemented bellow was tested on Android only so it
  82. // is considered safest to use it there only.
  83. return;
  84. }
  85. const oldHandler = global.ErrorUtils.getGlobalHandler();
  86. const newHandler = _handleException;
  87. if (!oldHandler || oldHandler !== newHandler) {
  88. newHandler.next = oldHandler;
  89. global.ErrorUtils.setGlobalHandler(newHandler);
  90. }
  91. }
  92. /**
  93. * Notified by React's Linking API that a specific URL registered to be
  94. * handled by this App was activated.
  95. *
  96. * @param {Object} event - The details of the notification/event.
  97. * @param {string} event.url - The URL registered to be handled by this App
  98. * which was activated.
  99. * @private
  100. * @returns {void}
  101. */
  102. _onLinkingURL(event) {
  103. this._openURL(event.url);
  104. }
  105. }
  106. /**
  107. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  108. * from converting a fatal error into an unhandled native exception which will
  109. * kill the application.
  110. *
  111. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  112. * @param {boolean} fatal - True if the specified error is fatal; otherwise,
  113. * false.
  114. * @private
  115. * @returns {void}
  116. */
  117. function _handleException(error, fatal) {
  118. if (fatal) {
  119. // In the Release configuration, React Native will (intentionally) throw
  120. // an unhandled JavascriptException for an unhandled JavaScript error.
  121. // This will effectively kill the application. In accord with the Web,
  122. // do not kill the application.
  123. console.error(error);
  124. } else {
  125. // Forward to the next globalHandler of ErrorUtils.
  126. const next = _handleException.next;
  127. typeof next === 'function' && next(error, fatal);
  128. }
  129. }