您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

App.native.js 4.9KB

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