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

AbstractApp.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* global APP */
  2. import _ from 'lodash';
  3. import PropTypes from 'prop-types';
  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 '../../base/i18n';
  10. import {
  11. MiddlewareRegistry,
  12. ReducerRegistry,
  13. StateListenerRegistry
  14. } from '../../base/redux';
  15. import { SoundCollection } from '../../base/sounds';
  16. import { PersistenceRegistry } from '../../base/storage';
  17. import { toURLString } from '../../base/util';
  18. import { OverlayContainer } from '../../overlay';
  19. import { appNavigate, appWillMount, appWillUnmount } from '../actions';
  20. /**
  21. * The default URL to open if no other was specified to {@code AbstractApp} via
  22. * props.
  23. *
  24. * FIXME: This is not at the best place here. This should be either in the
  25. * base/settings feature or a default in base/config.
  26. */
  27. const DEFAULT_URL = 'https://meet.jit.si';
  28. /**
  29. * Base (abstract) class for main App component.
  30. *
  31. * @abstract
  32. */
  33. export class AbstractApp extends Component {
  34. /**
  35. * {@code AbstractApp} component's property types.
  36. *
  37. * @static
  38. */
  39. static propTypes = {
  40. /**
  41. * The default URL {@code AbstractApp} is to open when not in any
  42. * conference/room.
  43. */
  44. defaultURL: PropTypes.string,
  45. // XXX Refer to the implementation of loadURLObject: in
  46. // ios/sdk/src/JitsiMeetView.m for further information.
  47. timestamp: PropTypes.any,
  48. /**
  49. * The URL, if any, with which the app was launched.
  50. */
  51. url: PropTypes.oneOfType([
  52. PropTypes.object,
  53. PropTypes.string
  54. ])
  55. };
  56. /**
  57. * Initializes a new {@code AbstractApp} instance.
  58. *
  59. * @param {Object} props - The read-only React {@code Component} props with
  60. * which the new instance is to be initialized.
  61. */
  62. constructor(props) {
  63. super(props);
  64. this.state = {
  65. /**
  66. * The state of the »possible« async initialization of the
  67. * {@code AbstractApp}.
  68. */
  69. appAsyncInitialized: false,
  70. /**
  71. * The Route rendered by this {@code AbstractApp}.
  72. *
  73. * @type {Route}
  74. */
  75. route: {},
  76. /**
  77. * The redux store used by this {@code AbstractApp}.
  78. *
  79. * @type {Store}
  80. */
  81. store: undefined
  82. };
  83. /**
  84. * Make the mobile {@code AbstractApp} wait until the
  85. * {@code AsyncStorage} implementation of {@code Storage} initializes
  86. * fully.
  87. *
  88. * @private
  89. * @see {@link #_initStorage}
  90. * @type {Promise}
  91. */
  92. this._init
  93. = this._initStorage()
  94. .catch(() => { /* AbstractApp should always initialize! */ })
  95. .then(() =>
  96. this.setState({
  97. store: this._createStore()
  98. }));
  99. }
  100. /**
  101. * Initializes the app.
  102. *
  103. * @inheritdoc
  104. */
  105. componentWillMount() {
  106. this._init.then(() => {
  107. const { dispatch } = this.state.store;
  108. dispatch(appWillMount(this));
  109. // We set the initialized state here and not in the constructor to
  110. // make sure that {@code componentWillMount} gets invoked before the
  111. // app tries to render the actual app content.
  112. this.setState({
  113. appAsyncInitialized: true
  114. });
  115. // If a URL was explicitly specified to this React Component, then
  116. // open it; otherwise, use a default.
  117. this._openURL(toURLString(this.props.url) || this._getDefaultURL());
  118. });
  119. }
  120. /**
  121. * Notifies this mounted React {@code Component} that it will receive new
  122. * props. Makes sure that this {@code AbstractApp} has a redux store to use.
  123. *
  124. * @inheritdoc
  125. * @param {Object} nextProps - The read-only React {@code Component} props
  126. * that this instance will receive.
  127. * @returns {void}
  128. */
  129. componentWillReceiveProps(nextProps) {
  130. const { props } = this;
  131. this._init.then(() => {
  132. // Deal with URL changes.
  133. let { url } = nextProps;
  134. url = toURLString(url);
  135. if (toURLString(props.url) !== url
  136. // XXX Refer to the implementation of loadURLObject: in
  137. // ios/sdk/src/JitsiMeetView.m for further information.
  138. || props.timestamp !== nextProps.timestamp) {
  139. this._openURL(url || this._getDefaultURL());
  140. }
  141. });
  142. }
  143. /**
  144. * De-initializes the app.
  145. *
  146. * @inheritdoc
  147. */
  148. componentWillUnmount() {
  149. this.state.store.dispatch(appWillUnmount(this));
  150. }
  151. /**
  152. * Gets a {@code Location} object from the window with information about the
  153. * current location of the document. Explicitly defined to allow extenders
  154. * to override because React Native does not usually have a location
  155. * property on its window unless debugging remotely in which case the
  156. * browser that is the remote debugger will provide a location property on
  157. * the window.
  158. *
  159. * @public
  160. * @returns {Location} A {@code Location} object with information about the
  161. * current location of the document.
  162. */
  163. getWindowLocation() {
  164. return undefined;
  165. }
  166. /**
  167. * Delays this {@code AbstractApp}'s startup until the {@code Storage}
  168. * implementation of {@code localStorage} initializes. While the
  169. * initialization is instantaneous on Web (with Web Storage API), it is
  170. * asynchronous on mobile/react-native.
  171. *
  172. * @private
  173. * @returns {Promise}
  174. */
  175. _initStorage() {
  176. const localStorageInitializing = window.localStorage._initializing;
  177. return (
  178. typeof localStorageInitializing === 'undefined'
  179. ? Promise.resolve()
  180. : localStorageInitializing);
  181. }
  182. /**
  183. * Implements React's {@link Component#render()}.
  184. *
  185. * @inheritdoc
  186. * @returns {ReactElement}
  187. */
  188. render() {
  189. const { appAsyncInitialized, route, store } = this.state;
  190. const { component } = route;
  191. if (appAsyncInitialized && component) {
  192. return (
  193. <I18nextProvider i18n = { i18next }>
  194. <Provider store = { store }>
  195. <Fragment>
  196. { this._createElement(component) }
  197. <SoundCollection />
  198. <OverlayContainer />
  199. </Fragment>
  200. </Provider>
  201. </I18nextProvider>
  202. );
  203. }
  204. return null;
  205. }
  206. /**
  207. * Creates a {@link ReactElement} from the specified component, the
  208. * specified props and the props of this {@code AbstractApp} which are
  209. * suitable for propagation to the children of this {@code Component}.
  210. *
  211. * @param {Component} component - The component from which the
  212. * {@code ReactElement} is to be created.
  213. * @param {Object} props - The read-only React {@code Component} props with
  214. * which the {@code ReactElement} is to be initialized.
  215. * @returns {ReactElement}
  216. * @protected
  217. */
  218. _createElement(component, props) {
  219. /* eslint-disable no-unused-vars */
  220. const {
  221. // The following props were introduced to be consumed entirely by
  222. // AbstractApp:
  223. defaultURL,
  224. timestamp,
  225. url,
  226. // The remaining props, if any, are considered suitable for
  227. // propagation to the children of this Component.
  228. ...thisProps
  229. } = this.props;
  230. /* eslint-enable no-unused-vars */
  231. return React.createElement(component, {
  232. ...thisProps,
  233. ...props
  234. });
  235. }
  236. /**
  237. * Initializes a new redux store instance suitable for use by this
  238. * {@code AbstractApp}.
  239. *
  240. * @private
  241. * @returns {Store} - A new redux store instance suitable for use by this
  242. * {@code AbstractApp}.
  243. */
  244. _createStore() {
  245. // Create combined reducer from all reducers in ReducerRegistry.
  246. const reducer = ReducerRegistry.combineReducers();
  247. // Apply all registered middleware from the MiddlewareRegistry and
  248. // additional 3rd party middleware:
  249. // - Thunk - allows us to dispatch async actions easily. For more info
  250. // @see https://github.com/gaearon/redux-thunk.
  251. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  252. // Try to enable Redux DevTools Chrome extension in order to make it
  253. // available for the purposes of facilitating development.
  254. let devToolsExtension;
  255. if (typeof window === 'object'
  256. && (devToolsExtension = window.devToolsExtension)) {
  257. middleware = compose(middleware, devToolsExtension());
  258. }
  259. const store
  260. = createStore(
  261. reducer,
  262. PersistenceRegistry.getPersistedState(),
  263. middleware);
  264. // StateListenerRegistry
  265. StateListenerRegistry.subscribe(store);
  266. // This is temporary workaround to be able to dispatch actions from
  267. // non-reactified parts of the code (conference.js for example).
  268. // Don't use in the react code!!!
  269. // FIXME: remove when the reactification is finished!
  270. if (typeof APP !== 'undefined') {
  271. APP.store = store;
  272. }
  273. return store;
  274. }
  275. /**
  276. * Gets the default URL to be opened when this {@code App} mounts.
  277. *
  278. * @protected
  279. * @returns {string} The default URL to be opened when this {@code App}
  280. * mounts.
  281. */
  282. _getDefaultURL() {
  283. // If the execution environment provides a Location abstraction, then
  284. // this App at already at that location but it must be made aware of the
  285. // fact.
  286. const windowLocation = this.getWindowLocation();
  287. if (windowLocation) {
  288. const href = windowLocation.toString();
  289. if (href) {
  290. return href;
  291. }
  292. }
  293. return (
  294. this.props.defaultURL
  295. || this.state.store.getState()['features/base/settings']
  296. .serverURL
  297. || DEFAULT_URL);
  298. }
  299. /**
  300. * Navigates to a specific Route.
  301. *
  302. * @param {Route} route - The Route to which to navigate.
  303. * @returns {Promise}
  304. */
  305. _navigate(route) {
  306. if (_.isEqual(route, this.state.route)) {
  307. return Promise.resolve();
  308. }
  309. if (route.href) {
  310. // This navigation requires loading a new URL in the browser.
  311. window.location.href = route.href;
  312. return Promise.resolve();
  313. }
  314. // XXX React's setState is asynchronous which means that the value of
  315. // this.state.route above may not even be correct. If the check is
  316. // performed before setState completes, the app may not navigate to the
  317. // expected route. In order to mitigate the problem, _navigate was
  318. // changed to return a Promise.
  319. return new Promise(resolve => this.setState({ route }, resolve));
  320. }
  321. /**
  322. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  323. *
  324. * @param {Object|string} url - The URL to navigate this {@code AbstractApp}
  325. * to (i.e. the URL to open).
  326. * @protected
  327. * @returns {void}
  328. */
  329. _openURL(url) {
  330. this.state.store.dispatch(appNavigate(toURLString(url)));
  331. }
  332. }