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 6.8KB

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