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.

BaseApp.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import _ from 'lodash';
  4. import React, { Component, Fragment } from 'react';
  5. import { I18nextProvider } from 'react-i18next';
  6. import { Provider } from 'react-redux';
  7. import { compose, createStore } from 'redux';
  8. import Thunk from 'redux-thunk';
  9. import { i18next } from '../../i18n';
  10. import {
  11. MiddlewareRegistry,
  12. PersistenceRegistry,
  13. ReducerRegistry,
  14. StateListenerRegistry
  15. } from '../../redux';
  16. import { SoundCollection } from '../../sounds';
  17. import { appWillMount, appWillUnmount } from '../actions';
  18. import logger from '../logger';
  19. declare var APP: Object;
  20. /**
  21. * The type of the React {@code Component} state of {@link BaseApp}.
  22. */
  23. type State = {
  24. /**
  25. * The {@code Route} rendered by the {@code BaseApp}.
  26. */
  27. route: Object,
  28. /**
  29. * The redux store used by the {@code BaseApp}.
  30. */
  31. store: Object
  32. };
  33. /**
  34. * Base (abstract) class for main App component.
  35. *
  36. * @abstract
  37. */
  38. export default class BaseApp extends Component<*, State> {
  39. _init: Promise<*>;
  40. /**
  41. * Initializes a new {@code BaseApp} instance.
  42. *
  43. * @param {Object} props - The read-only React {@code Component} props with
  44. * which the new instance is to be initialized.
  45. */
  46. constructor(props: Object) {
  47. super(props);
  48. this.state = {
  49. route: {},
  50. store: undefined
  51. };
  52. }
  53. /**
  54. * Initializes the app.
  55. *
  56. * @inheritdoc
  57. */
  58. componentDidMount() {
  59. /**
  60. * Make the mobile {@code BaseApp} wait until the {@code AsyncStorage}
  61. * implementation of {@code Storage} initializes fully.
  62. *
  63. * @private
  64. * @see {@link #_initStorage}
  65. * @type {Promise}
  66. */
  67. this._init = this._initStorage()
  68. .catch(err => {
  69. /* BaseApp should always initialize! */
  70. logger.error(err);
  71. })
  72. .then(() => new Promise(resolve => {
  73. this.setState({
  74. store: this._createStore()
  75. }, resolve);
  76. }))
  77. .then(() => this.state.store.dispatch(appWillMount(this)))
  78. .catch(err => {
  79. /* BaseApp should always initialize! */
  80. logger.error(err);
  81. });
  82. }
  83. /**
  84. * De-initializes the app.
  85. *
  86. * @inheritdoc
  87. */
  88. componentWillUnmount() {
  89. this.state.store.dispatch(appWillUnmount(this));
  90. }
  91. /**
  92. * Logs for errors that were not caught.
  93. *
  94. * @param {Error} error - The error that was thrown.
  95. * @param {Object} info - Info about the error(stack trace);.
  96. *
  97. * @returns {void}
  98. */
  99. componentDidCatch(error: Error, info: Object) {
  100. logger.error(error, info);
  101. }
  102. /**
  103. * Delays this {@code BaseApp}'s startup until the {@code Storage}
  104. * implementation of {@code localStorage} initializes. While the
  105. * initialization is instantaneous on Web (with Web Storage API), it is
  106. * asynchronous on mobile/react-native.
  107. *
  108. * @private
  109. * @returns {Promise}
  110. */
  111. _initStorage(): Promise<*> {
  112. const _initializing = jitsiLocalStorage.getItem('_initializing');
  113. return _initializing || Promise.resolve();
  114. }
  115. /**
  116. * Implements React's {@link Component#render()}.
  117. *
  118. * @inheritdoc
  119. * @returns {ReactElement}
  120. */
  121. render() {
  122. const { route: { component, props }, store } = this.state;
  123. if (store) {
  124. return (
  125. <I18nextProvider i18n = { i18next }>
  126. <Provider store = { store }>
  127. <Fragment>
  128. { this._createMainElement(component, props) }
  129. <SoundCollection />
  130. { this._createExtraElement() }
  131. { this._renderDialogContainer() }
  132. </Fragment>
  133. </Provider>
  134. </I18nextProvider>
  135. );
  136. }
  137. return null;
  138. }
  139. /**
  140. * Creates an extra {@link ReactElement}s to be added (unconditionally)
  141. * alongside the main element.
  142. *
  143. * @returns {ReactElement}
  144. * @abstract
  145. * @protected
  146. */
  147. _createExtraElement() {
  148. return null;
  149. }
  150. /**
  151. * Creates a {@link ReactElement} from the specified component, the
  152. * specified props and the props of this {@code AbstractApp} which are
  153. * suitable for propagation to the children of this {@code Component}.
  154. *
  155. * @param {Component} component - The component from which the
  156. * {@code ReactElement} is to be created.
  157. * @param {Object} props - The read-only React {@code Component} props with
  158. * which the {@code ReactElement} is to be initialized.
  159. * @returns {ReactElement}
  160. * @protected
  161. */
  162. _createMainElement(component, props) {
  163. return component ? React.createElement(component, props || {}) : null;
  164. }
  165. /**
  166. * Initializes a new redux store instance suitable for use by this
  167. * {@code AbstractApp}.
  168. *
  169. * @private
  170. * @returns {Store} - A new redux store instance suitable for use by
  171. * this {@code AbstractApp}.
  172. */
  173. _createStore() {
  174. // Create combined reducer from all reducers in ReducerRegistry.
  175. const reducer = ReducerRegistry.combineReducers();
  176. // Apply all registered middleware from the MiddlewareRegistry and
  177. // additional 3rd party middleware:
  178. // - Thunk - allows us to dispatch async actions easily. For more info
  179. // @see https://github.com/gaearon/redux-thunk.
  180. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  181. // Try to enable Redux DevTools Chrome extension in order to make it
  182. // available for the purposes of facilitating development.
  183. let devToolsExtension;
  184. if (typeof window === 'object'
  185. && (devToolsExtension = window.devToolsExtension)) {
  186. middleware = compose(middleware, devToolsExtension());
  187. }
  188. const store = createStore(
  189. reducer, PersistenceRegistry.getPersistedState(), middleware);
  190. // StateListenerRegistry
  191. StateListenerRegistry.subscribe(store);
  192. // This is temporary workaround to be able to dispatch actions from
  193. // non-reactified parts of the code (conference.js for example).
  194. // Don't use in the react code!!!
  195. // FIXME: remove when the reactification is finished!
  196. if (typeof APP !== 'undefined') {
  197. APP.store = store;
  198. }
  199. return store;
  200. }
  201. /**
  202. * Navigates to a specific Route.
  203. *
  204. * @param {Route} route - The Route to which to navigate.
  205. * @returns {Promise}
  206. */
  207. _navigate(route): Promise<*> {
  208. if (_.isEqual(route, this.state.route)) {
  209. return Promise.resolve();
  210. }
  211. if (route.href) {
  212. // This navigation requires loading a new URL in the browser.
  213. window.location.href = route.href;
  214. return Promise.resolve();
  215. }
  216. // XXX React's setState is asynchronous which means that the value of
  217. // this.state.route above may not even be correct. If the check is
  218. // performed before setState completes, the app may not navigate to the
  219. // expected route. In order to mitigate the problem, _navigate was
  220. // changed to return a Promise.
  221. return new Promise(resolve => {
  222. this.setState({ route }, resolve);
  223. });
  224. }
  225. /**
  226. * Renders the platform specific dialog container.
  227. *
  228. * @returns {React$Element}
  229. */
  230. _renderDialogContainer: () => React$Element<*>
  231. }