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

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