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

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