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

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