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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // @flow
  2. import React from 'react';
  3. import { setColorScheme } from '../../base/color-scheme';
  4. import { DialogContainer } from '../../base/dialog';
  5. import { CALL_INTEGRATION_ENABLED, updateFlags } from '../../base/flags';
  6. import { Platform } from '../../base/react';
  7. import { DimensionsDetector, clientResized } from '../../base/responsive-ui';
  8. import { updateSettings } from '../../base/settings';
  9. import logger from '../logger';
  10. import { AbstractApp } from './AbstractApp';
  11. import type { Props as AbstractAppProps } from './AbstractApp';
  12. // Register middlewares and reducers.
  13. import '../middlewares';
  14. import '../reducers';
  15. declare var __DEV__;
  16. /**
  17. * The type of React {@code Component} props of {@link App}.
  18. */
  19. type Props = AbstractAppProps & {
  20. /**
  21. * An object of colors that override the default colors of the app/sdk.
  22. */
  23. colorScheme: ?Object,
  24. /**
  25. * Identifier for this app on the native side.
  26. */
  27. externalAPIScope: string,
  28. /**
  29. * An object with the feature flags.
  30. */
  31. flags: Object,
  32. /**
  33. * An object with user information (display name, email, avatar URL).
  34. */
  35. userInfo: ?Object
  36. };
  37. /**
  38. * Root app {@code Component} on mobile/React Native.
  39. *
  40. * @extends AbstractApp
  41. */
  42. export class App extends AbstractApp {
  43. _init: Promise<*>;
  44. /**
  45. * Initializes a new {@code App} instance.
  46. *
  47. * @param {Props} props - The read-only React {@code Component} props with
  48. * which the new instance is to be initialized.
  49. */
  50. constructor(props: Props) {
  51. super(props);
  52. // In the Release configuration, React Native will (intentionally) throw
  53. // an unhandled JavascriptException for an unhandled JavaScript error.
  54. // This will effectively kill the app. In accord with the Web, do not
  55. // kill the app.
  56. this._maybeDisableExceptionsManager();
  57. // Bind event handler so it is only bound once per instance.
  58. this._onDimensionsChanged = this._onDimensionsChanged.bind(this);
  59. }
  60. /**
  61. * Initializes the color scheme.
  62. *
  63. * @inheritdoc
  64. *
  65. * @returns {void}
  66. */
  67. componentDidMount() {
  68. super.componentDidMount();
  69. this._init.then(() => {
  70. // We set these early enough so then we avoid any unnecessary re-renders.
  71. const { dispatch } = this.state.store;
  72. dispatch(setColorScheme(this.props.colorScheme));
  73. dispatch(updateFlags(this.props.flags));
  74. dispatch(updateSettings(this.props.userInfo || {}));
  75. // Update settings with feature-flag.
  76. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  77. if (typeof callIntegrationEnabled !== 'undefined') {
  78. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  79. }
  80. });
  81. }
  82. /**
  83. * Overrides the parent method to inject {@link DimensionsDetector} as
  84. * the top most component.
  85. *
  86. * @override
  87. */
  88. _createMainElement(component, props) {
  89. return (
  90. <DimensionsDetector
  91. onDimensionsChanged = { this._onDimensionsChanged }>
  92. { super._createMainElement(component, props) }
  93. </DimensionsDetector>
  94. );
  95. }
  96. /**
  97. * Attempts to disable the use of React Native
  98. * {@link ExceptionsManager#handleException} on platforms and in
  99. * configurations on/in which the use of the method in questions has been
  100. * determined to be undesirable. For example, React Native will
  101. * (intentionally) throw an unhandled {@code JavascriptException} for an
  102. * unhandled JavaScript error in the Release configuration. This will
  103. * effectively kill the app. In accord with the Web, do not kill the app.
  104. *
  105. * @private
  106. * @returns {void}
  107. */
  108. _maybeDisableExceptionsManager() {
  109. if (__DEV__) {
  110. // As mentioned above, only the Release configuration was observed
  111. // to suffer.
  112. return;
  113. }
  114. if (Platform.OS !== 'android') {
  115. // A solution based on RTCSetFatalHandler was implemented on iOS and
  116. // it is preferred because it is at a later step of the
  117. // error/exception handling and it is specific to fatal
  118. // errors/exceptions which were observed to kill the app. The
  119. // solution implemented bellow was tested on Android only so it is
  120. // considered safest to use it there only.
  121. return;
  122. }
  123. const oldHandler = global.ErrorUtils.getGlobalHandler();
  124. const newHandler = _handleException;
  125. if (!oldHandler || oldHandler !== newHandler) {
  126. newHandler.next = oldHandler;
  127. global.ErrorUtils.setGlobalHandler(newHandler);
  128. }
  129. }
  130. _onDimensionsChanged: (width: number, height: number) => void;
  131. /**
  132. * Updates the known available size for the app to occupy.
  133. *
  134. * @param {number} width - The component's current width.
  135. * @param {number} height - The component's current height.
  136. * @private
  137. * @returns {void}
  138. */
  139. _onDimensionsChanged(width: number, height: number) {
  140. const { dispatch } = this.state.store;
  141. dispatch(clientResized(width, height));
  142. }
  143. /**
  144. * Renders the platform specific dialog container.
  145. *
  146. * @returns {React$Element}
  147. */
  148. _renderDialogContainer() {
  149. return (
  150. <DialogContainer />
  151. );
  152. }
  153. }
  154. /**
  155. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  156. * from converting a fatal error into an unhandled native exception which will
  157. * kill the app.
  158. *
  159. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  160. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  161. * otherwise, {@code false}.
  162. * @private
  163. * @returns {void}
  164. */
  165. function _handleException(error, fatal) {
  166. if (fatal) {
  167. // In the Release configuration, React Native will (intentionally) throw
  168. // an unhandled JavascriptException for an unhandled JavaScript error.
  169. // This will effectively kill the app. In accord with the Web, do not
  170. // kill the app.
  171. logger.error(error);
  172. } else {
  173. // Forward to the next globalHandler of ErrorUtils.
  174. const { next } = _handleException;
  175. typeof next === 'function' && next(error, fatal);
  176. }
  177. }