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

App.native.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // @flow
  2. import React from 'react';
  3. import { setColorScheme } from '../../base/color-scheme';
  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. * @extends AbstractApp
  43. */
  44. export class App extends AbstractApp {
  45. _init: Promise<*>;
  46. /**
  47. * Initializes a new {@code App} instance.
  48. *
  49. * @param {Props} props - The read-only React {@code Component} props with
  50. * which the new instance is to be initialized.
  51. */
  52. constructor(props: Props) {
  53. super(props);
  54. // In the Release configuration, React Native will (intentionally) throw
  55. // an unhandled JavascriptException for an unhandled JavaScript error.
  56. // This will effectively kill the app. In accord with the Web, do not
  57. // kill the app.
  58. this._maybeDisableExceptionsManager();
  59. // Bind event handler so it is only bound once per instance.
  60. this._onDimensionsChanged = this._onDimensionsChanged.bind(this);
  61. }
  62. /**
  63. * Initializes the color scheme.
  64. *
  65. * @inheritdoc
  66. *
  67. * @returns {void}
  68. */
  69. componentDidMount() {
  70. super.componentDidMount();
  71. this._init.then(() => {
  72. const { dispatch, getState } = this.state.store;
  73. // We set these early enough so then we avoid any unnecessary re-renders.
  74. dispatch(setColorScheme(this.props.colorScheme));
  75. dispatch(updateFlags(this.props.flags));
  76. // Check if serverURL is configured externally and not allowed to change.
  77. const serverURLChangeEnabled = getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
  78. if (!serverURLChangeEnabled) {
  79. // As serverURL is provided externally, so we push it to settings.
  80. if (typeof this.props.url !== 'undefined') {
  81. const { serverURL } = this.props.url;
  82. if (typeof serverURL !== 'undefined') {
  83. dispatch(updateSettings({ serverURL }));
  84. }
  85. }
  86. }
  87. dispatch(updateSettings(this.props.userInfo || {}));
  88. // Update settings with feature-flag.
  89. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  90. if (typeof callIntegrationEnabled !== 'undefined') {
  91. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  92. }
  93. });
  94. }
  95. /**
  96. * Overrides the parent method to inject {@link DimensionsDetector} as
  97. * the top most component.
  98. *
  99. * @override
  100. */
  101. _createMainElement(component, props) {
  102. return (
  103. <DimensionsDetector
  104. onDimensionsChanged = { this._onDimensionsChanged }>
  105. { super._createMainElement(component, props) }
  106. </DimensionsDetector>
  107. );
  108. }
  109. /**
  110. * Attempts to disable the use of React Native
  111. * {@link ExceptionsManager#handleException} on platforms and in
  112. * configurations on/in which the use of the method in questions has been
  113. * determined to be undesirable. For example, React Native will
  114. * (intentionally) throw an unhandled {@code JavascriptException} for an
  115. * unhandled JavaScript error in the Release configuration. This will
  116. * effectively kill the app. In accord with the Web, do not kill the app.
  117. *
  118. * @private
  119. * @returns {void}
  120. */
  121. _maybeDisableExceptionsManager() {
  122. if (__DEV__) {
  123. // As mentioned above, only the Release configuration was observed
  124. // to suffer.
  125. return;
  126. }
  127. if (Platform.OS !== 'android') {
  128. // A solution based on RTCSetFatalHandler was implemented on iOS and
  129. // it is preferred because it is at a later step of the
  130. // error/exception handling and it is specific to fatal
  131. // errors/exceptions which were observed to kill the app. The
  132. // solution implemented bellow was tested on Android only so it is
  133. // considered safest to use it there only.
  134. return;
  135. }
  136. const oldHandler = global.ErrorUtils.getGlobalHandler();
  137. const newHandler = _handleException;
  138. if (!oldHandler || oldHandler !== newHandler) {
  139. newHandler.next = oldHandler;
  140. global.ErrorUtils.setGlobalHandler(newHandler);
  141. }
  142. }
  143. _onDimensionsChanged: (width: number, height: number) => void;
  144. /**
  145. * Updates the known available size for the app to occupy.
  146. *
  147. * @param {number} width - The component's current width.
  148. * @param {number} height - The component's current height.
  149. * @private
  150. * @returns {void}
  151. */
  152. _onDimensionsChanged(width: number, height: number) {
  153. const { dispatch } = this.state.store;
  154. dispatch(clientResized(width, height));
  155. }
  156. /**
  157. * Renders the platform specific dialog container.
  158. *
  159. * @returns {React$Element}
  160. */
  161. _renderDialogContainer() {
  162. return (
  163. <DialogContainer />
  164. );
  165. }
  166. }
  167. /**
  168. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  169. * from converting a fatal error into an unhandled native exception which will
  170. * kill the app.
  171. *
  172. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  173. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  174. * otherwise, {@code false}.
  175. * @private
  176. * @returns {void}
  177. */
  178. function _handleException(error, fatal) {
  179. if (fatal) {
  180. // In the Release configuration, React Native will (intentionally) throw
  181. // an unhandled JavascriptException for an unhandled JavaScript error.
  182. // This will effectively kill the app. In accord with the Web, do not
  183. // kill the app.
  184. logger.error(error);
  185. } else {
  186. // Forward to the next globalHandler of ErrorUtils.
  187. const { next } = _handleException;
  188. typeof next === 'function' && next(error, fatal);
  189. }
  190. }