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

BaseApp.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // @flow
  2. import { jitsiLocalStorage } from '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. ReducerRegistry,
  13. StateListenerRegistry
  14. } from '../../redux';
  15. import { SoundCollection } from '../../sounds';
  16. import { PersistenceRegistry } from '../../storage';
  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. * Delays this {@code BaseApp}'s startup until the {@code Storage}
  93. * implementation of {@code localStorage} initializes. While the
  94. * initialization is instantaneous on Web (with Web Storage API), it is
  95. * asynchronous on mobile/react-native.
  96. *
  97. * @private
  98. * @returns {Promise}
  99. */
  100. _initStorage(): Promise<*> {
  101. const _initializing = jitsiLocalStorage.getItem('_initializing');
  102. return _initializing || Promise.resolve();
  103. }
  104. /**
  105. * Implements React's {@link Component#render()}.
  106. *
  107. * @inheritdoc
  108. * @returns {ReactElement}
  109. */
  110. render() {
  111. const { route: { component }, store } = this.state;
  112. if (store) {
  113. return (
  114. <I18nextProvider i18n = { i18next }>
  115. <Provider store = { store }>
  116. <Fragment>
  117. { this._createMainElement(component) }
  118. <SoundCollection />
  119. { this._createExtraElement() }
  120. { this._renderDialogContainer() }
  121. </Fragment>
  122. </Provider>
  123. </I18nextProvider>
  124. );
  125. }
  126. return null;
  127. }
  128. /**
  129. * Creates an extra {@link ReactElement}s to be added (unconditionaly)
  130. * alongside the main element.
  131. *
  132. * @returns {ReactElement}
  133. * @abstract
  134. * @protected
  135. */
  136. _createExtraElement() {
  137. return null;
  138. }
  139. /**
  140. * Creates a {@link ReactElement} from the specified component, the
  141. * specified props and the props of this {@code AbstractApp} which are
  142. * suitable for propagation to the children of this {@code Component}.
  143. *
  144. * @param {Component} component - The component from which the
  145. * {@code ReactElement} is to be created.
  146. * @param {Object} props - The read-only React {@code Component} props with
  147. * which the {@code ReactElement} is to be initialized.
  148. * @returns {ReactElement}
  149. * @protected
  150. */
  151. _createMainElement(component, props) {
  152. return component ? React.createElement(component, props || {}) : null;
  153. }
  154. /**
  155. * Initializes a new redux store instance suitable for use by this
  156. * {@code AbstractApp}.
  157. *
  158. * @private
  159. * @returns {Store} - A new redux store instance suitable for use by
  160. * this {@code AbstractApp}.
  161. */
  162. _createStore() {
  163. // Create combined reducer from all reducers in ReducerRegistry.
  164. const reducer = ReducerRegistry.combineReducers();
  165. // Apply all registered middleware from the MiddlewareRegistry and
  166. // additional 3rd party middleware:
  167. // - Thunk - allows us to dispatch async actions easily. For more info
  168. // @see https://github.com/gaearon/redux-thunk.
  169. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  170. // Try to enable Redux DevTools Chrome extension in order to make it
  171. // available for the purposes of facilitating development.
  172. let devToolsExtension;
  173. if (typeof window === 'object'
  174. && (devToolsExtension = window.devToolsExtension)) {
  175. middleware = compose(middleware, devToolsExtension());
  176. }
  177. const store = createStore(
  178. reducer, PersistenceRegistry.getPersistedState(), middleware);
  179. // StateListenerRegistry
  180. StateListenerRegistry.subscribe(store);
  181. // This is temporary workaround to be able to dispatch actions from
  182. // non-reactified parts of the code (conference.js for example).
  183. // Don't use in the react code!!!
  184. // FIXME: remove when the reactification is finished!
  185. if (typeof APP !== 'undefined') {
  186. APP.store = store;
  187. }
  188. return store;
  189. }
  190. /**
  191. * Navigates to a specific Route.
  192. *
  193. * @param {Route} route - The Route to which to navigate.
  194. * @returns {Promise}
  195. */
  196. _navigate(route): Promise<*> {
  197. if (_.isEqual(route, this.state.route)) {
  198. return Promise.resolve();
  199. }
  200. if (route.href) {
  201. // This navigation requires loading a new URL in the browser.
  202. window.location.href = route.href;
  203. return Promise.resolve();
  204. }
  205. // XXX React's setState is asynchronous which means that the value of
  206. // this.state.route above may not even be correct. If the check is
  207. // performed before setState completes, the app may not navigate to the
  208. // expected route. In order to mitigate the problem, _navigate was
  209. // changed to return a Promise.
  210. return new Promise(resolve => {
  211. this.setState({ route }, resolve);
  212. });
  213. }
  214. /**
  215. * Renders the platform specific dialog container.
  216. *
  217. * @returns {React$Element}
  218. */
  219. _renderDialogContainer: () => React$Element<*>
  220. }