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

AbstractApp.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /**
  46. * (Optional) redux store for this app.
  47. */
  48. store: PropTypes.object,
  49. // XXX Refer to the implementation of loadURLObject: in
  50. // ios/sdk/src/JitsiMeetView.m for further information.
  51. timestamp: PropTypes.any,
  52. /**
  53. * The URL, if any, with which the app was launched.
  54. */
  55. url: PropTypes.oneOfType([
  56. PropTypes.object,
  57. PropTypes.string
  58. ])
  59. };
  60. /**
  61. * Initializes a new {@code AbstractApp} instance.
  62. *
  63. * @param {Object} props - The read-only React {@code Component} props with
  64. * which the new instance is to be initialized.
  65. */
  66. constructor(props) {
  67. super(props);
  68. this.state = {
  69. /**
  70. * The state of the »possible« async initialization of
  71. * the {@code AbstractApp}.
  72. */
  73. appAsyncInitialized: false,
  74. /**
  75. * The Route rendered by this {@code AbstractApp}.
  76. *
  77. * @type {Route}
  78. */
  79. route: {},
  80. /**
  81. * The redux store used by this {@code AbstractApp}.
  82. *
  83. * @type {Store}
  84. */
  85. store: undefined
  86. };
  87. /**
  88. * Make the mobile {@code AbstractApp} wait until the
  89. * {@code AsyncStorage} implementation of {@code Storage} initializes
  90. * fully.
  91. *
  92. * @private
  93. * @see {@link #_initStorage}
  94. * @type {Promise}
  95. */
  96. this._init
  97. = this._initStorage()
  98. .catch(() => { /* AbstractApp should always initialize! */ })
  99. .then(() =>
  100. this.setState({
  101. store: this._maybeCreateStore(props)
  102. }));
  103. }
  104. /**
  105. * Initializes the app.
  106. *
  107. * @inheritdoc
  108. */
  109. componentWillMount() {
  110. this._init.then(() => {
  111. const { dispatch } = this._getStore();
  112. dispatch(appWillMount(this));
  113. // We set the initialized state here and not in the constructor to
  114. // make sure that {@code componentWillMount} gets invoked before
  115. // the app tries to render the actual app content.
  116. this.setState({
  117. appAsyncInitialized: true
  118. });
  119. // If a URL was explicitly specified to this React Component,
  120. // then open it; otherwise, use a default.
  121. this._openURL(toURLString(this.props.url) || this._getDefaultURL());
  122. });
  123. }
  124. /**
  125. * Notifies this mounted React {@code Component} that it will receive new
  126. * props. Makes sure that this {@code AbstractApp} has a redux store to use.
  127. *
  128. * @inheritdoc
  129. * @param {Object} nextProps - The read-only React {@code Component} props
  130. * that this instance will receive.
  131. * @returns {void}
  132. */
  133. componentWillReceiveProps(nextProps) {
  134. const { props } = this;
  135. this._init.then(() => {
  136. // The consumer of this AbstractApp did not provide a redux store.
  137. if (typeof nextProps.store === 'undefined'
  138. // The consumer of this AbstractApp did provide a redux
  139. // store before. Which means that the consumer changed
  140. // their mind. In such a case this instance should create
  141. // its own internal redux store. If the consumer did not
  142. // provide a redux store before, then this instance is
  143. // using its own internal redux store already.
  144. && typeof props.store !== 'undefined') {
  145. this.setState({
  146. store: this._maybeCreateStore(nextProps)
  147. });
  148. }
  149. // Deal with URL changes.
  150. let { url } = nextProps;
  151. url = toURLString(url);
  152. if (toURLString(props.url) !== url
  153. // XXX Refer to the implementation of loadURLObject: in
  154. // ios/sdk/src/JitsiMeetView.m for further information.
  155. || props.timestamp !== nextProps.timestamp) {
  156. this._openURL(url || this._getDefaultURL());
  157. }
  158. });
  159. }
  160. /**
  161. * De-initializes the app.
  162. *
  163. * @inheritdoc
  164. */
  165. componentWillUnmount() {
  166. const { dispatch } = this._getStore();
  167. dispatch(appWillUnmount(this));
  168. }
  169. /**
  170. * Gets a {@code Location} object from the window with information about the
  171. * current location of the document. Explicitly defined to allow extenders
  172. * to override because React Native does not usually have a location
  173. * property on its window unless debugging remotely in which case the
  174. * browser that is the remote debugger will provide a location property on
  175. * the window.
  176. *
  177. * @public
  178. * @returns {Location} A {@code Location} object with information about the
  179. * current location of the document.
  180. */
  181. getWindowLocation() {
  182. return undefined;
  183. }
  184. /**
  185. * Delays this {@code AbstractApp}'s startup until the {@code Storage}
  186. * implementation of {@code localStorage} initializes. While the
  187. * initialization is instantaneous on Web (with Web Storage API), it is
  188. * asynchronous on mobile/react-native.
  189. *
  190. * @private
  191. * @returns {Promise}
  192. */
  193. _initStorage() {
  194. const localStorageInitializing = window.localStorage._initializing;
  195. return (
  196. typeof localStorageInitializing === 'undefined'
  197. ? Promise.resolve()
  198. : localStorageInitializing);
  199. }
  200. /**
  201. * Implements React's {@link Component#render()}.
  202. *
  203. * @inheritdoc
  204. * @returns {ReactElement}
  205. */
  206. render() {
  207. const { appAsyncInitialized, route: { component } } = this.state;
  208. if (appAsyncInitialized && component) {
  209. return (
  210. <I18nextProvider i18n = { i18next }>
  211. <Provider store = { this._getStore() }>
  212. <Fragment>
  213. { this._createElement(component) }
  214. <SoundCollection />
  215. <OverlayContainer />
  216. </Fragment>
  217. </Provider>
  218. </I18nextProvider>
  219. );
  220. }
  221. return null;
  222. }
  223. /**
  224. * Creates a {@link ReactElement} from the specified component, the
  225. * specified props and the props of this {@code AbstractApp} which are
  226. * suitable for propagation to the children of this {@code Component}.
  227. *
  228. * @param {Component} component - The component from which the
  229. * {@code ReactElement} is to be created.
  230. * @param {Object} props - The read-only React {@code Component} props with
  231. * which the {@code ReactElement} is to be initialized.
  232. * @returns {ReactElement}
  233. * @protected
  234. */
  235. _createElement(component, props) {
  236. /* eslint-disable no-unused-vars */
  237. const {
  238. // Don't propagate the dispatch and store props because they usually
  239. // come from react-redux and programmers don't really expect them to
  240. // be inherited but rather explicitly connected.
  241. dispatch, // eslint-disable-line react/prop-types
  242. store,
  243. // The following props were introduced to be consumed entirely by
  244. // AbstractApp:
  245. defaultURL,
  246. url,
  247. // The remaining props, if any, are considered suitable for
  248. // propagation to the children of this Component.
  249. ...thisProps
  250. } = this.props;
  251. /* eslint-enable no-unused-vars */
  252. return React.createElement(component, {
  253. ...thisProps,
  254. ...props
  255. });
  256. }
  257. /**
  258. * Initializes a new redux store instance suitable for use by this
  259. * {@code AbstractApp}.
  260. *
  261. * @private
  262. * @returns {Store} - A new redux store instance suitable for use by
  263. * this {@code AbstractApp}.
  264. */
  265. _createStore() {
  266. // Create combined reducer from all reducers in ReducerRegistry.
  267. const reducer = ReducerRegistry.combineReducers();
  268. // Apply all registered middleware from the MiddlewareRegistry and
  269. // additional 3rd party middleware:
  270. // - Thunk - allows us to dispatch async actions easily. For more info
  271. // @see https://github.com/gaearon/redux-thunk.
  272. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  273. // Try to enable Redux DevTools Chrome extension in order to make it
  274. // available for the purposes of facilitating development.
  275. let devToolsExtension;
  276. if (typeof window === 'object'
  277. && (devToolsExtension = window.devToolsExtension)) {
  278. middleware = compose(middleware, devToolsExtension());
  279. }
  280. return (
  281. createStore(
  282. reducer,
  283. PersistenceRegistry.getPersistedState(),
  284. middleware));
  285. }
  286. /**
  287. * Gets the default URL to be opened when this {@code App} mounts.
  288. *
  289. * @protected
  290. * @returns {string} The default URL to be opened when this {@code App}
  291. * mounts.
  292. */
  293. _getDefaultURL() {
  294. // If the execution environment provides a Location abstraction, then
  295. // this App at already at that location but it must be made aware of the
  296. // fact.
  297. const windowLocation = this.getWindowLocation();
  298. if (windowLocation) {
  299. const href = windowLocation.toString();
  300. if (href) {
  301. return href;
  302. }
  303. }
  304. return (
  305. this.props.defaultURL
  306. || this._getStore().getState()['features/base/settings']
  307. .serverURL
  308. || DEFAULT_URL);
  309. }
  310. /**
  311. * Gets the redux store used by this {@code AbstractApp}.
  312. *
  313. * @protected
  314. * @returns {Store} - The redux store used by this {@code AbstractApp}.
  315. */
  316. _getStore() {
  317. let store = this.state.store;
  318. if (typeof store === 'undefined') {
  319. store = this.props.store;
  320. }
  321. return store;
  322. }
  323. /**
  324. * Creates a redux store to be used by this {@code AbstractApp} if such as a
  325. * store is not defined by the consumer of this {@code AbstractApp} through
  326. * its read-only React {@code Component} props.
  327. *
  328. * @param {Object} props - The read-only React {@code Component} props that
  329. * will eventually be received by this {@code AbstractApp}.
  330. * @private
  331. * @returns {Store} - The redux store to be used by this
  332. * {@code AbstractApp}.
  333. */
  334. _maybeCreateStore({ store }) {
  335. // The application Jitsi Meet is architected with redux. However, I do
  336. // not want consumers of the App React Component to be forced into
  337. // dealing with redux. If the consumer did not provide an external redux
  338. // store, utilize an internal redux store.
  339. if (typeof store === 'undefined') {
  340. // eslint-disable-next-line no-param-reassign
  341. store = this._createStore();
  342. // This is temporary workaround to be able to dispatch actions from
  343. // non-reactified parts of the code (conference.js for example).
  344. // Don't use in the react code!!!
  345. // FIXME: remove when the reactification is finished!
  346. if (typeof APP !== 'undefined') {
  347. APP.store = store;
  348. }
  349. }
  350. // StateListenerRegistry
  351. store && StateListenerRegistry.subscribe(store);
  352. return store;
  353. }
  354. /**
  355. * Navigates to a specific Route.
  356. *
  357. * @param {Route} route - The Route to which to navigate.
  358. * @returns {Promise}
  359. */
  360. _navigate(route) {
  361. if (_.isEqual(route, this.state.route)) {
  362. return Promise.resolve();
  363. }
  364. if (route.href) {
  365. // This navigation requires loading a new URL in the browser.
  366. window.location.href = route.href;
  367. return Promise.resolve();
  368. }
  369. // XXX React's setState is asynchronous which means that the value of
  370. // this.state.route above may not even be correct. If the check is
  371. // performed before setState completes, the app may not navigate to the
  372. // expected route. In order to mitigate the problem, _navigate was
  373. // changed to return a Promise.
  374. return new Promise(resolve => this.setState({ route }, resolve));
  375. }
  376. /**
  377. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  378. *
  379. * @param {Object|string} url - The URL to navigate this {@code AbstractApp}
  380. * to (i.e. the URL to open).
  381. * @protected
  382. * @returns {void}
  383. */
  384. _openURL(url) {
  385. this._getStore().dispatch(appNavigate(toURLString(url)));
  386. }
  387. }