Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

App.native.js 7.1KB

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