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.1KB

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