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

AbstractApp.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* global APP */
  2. import PropTypes from 'prop-types';
  3. import React, { Component } 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 '../../base/i18n';
  9. import { localParticipantLeft } from '../../base/participants';
  10. import { Fragment, RouteRegistry } from '../../base/react';
  11. import { MiddlewareRegistry, ReducerRegistry } from '../../base/redux';
  12. import { SoundCollection } from '../../base/sounds';
  13. import { PersistenceRegistry } from '../../base/storage';
  14. import { toURLString } from '../../base/util';
  15. import { OverlayContainer } from '../../overlay';
  16. import { BlankPage } from '../../welcome';
  17. import { appNavigate, appWillMount, appWillUnmount } from '../actions';
  18. /**
  19. * The default URL to open if no other was specified to {@code AbstractApp}
  20. * via props.
  21. *
  22. * FIXME: This is not at the best place here. This should be either in the
  23. * base/settings feature or a default in base/config.
  24. */
  25. const DEFAULT_URL = 'https://meet.jit.si';
  26. /**
  27. * Base (abstract) class for main App component.
  28. *
  29. * @abstract
  30. */
  31. export class AbstractApp extends Component {
  32. /**
  33. * {@code AbstractApp} component's property types.
  34. *
  35. * @static
  36. */
  37. static propTypes = {
  38. /**
  39. * The default URL {@code AbstractApp} is to open when not in any
  40. * conference/room.
  41. */
  42. defaultURL: PropTypes.string,
  43. /**
  44. * (Optional) redux store for this app.
  45. */
  46. store: PropTypes.object,
  47. // XXX Refer to the implementation of loadURLObject: in
  48. // ios/sdk/src/JitsiMeetView.m for further information.
  49. timestamp: PropTypes.any,
  50. /**
  51. * The URL, if any, with which the app was launched.
  52. */
  53. url: PropTypes.oneOfType([
  54. PropTypes.object,
  55. PropTypes.string
  56. ])
  57. };
  58. /**
  59. * Initializes a new {@code AbstractApp} instance.
  60. *
  61. * @param {Object} props - The read-only React {@code Component} props with
  62. * which the new instance is to be initialized.
  63. */
  64. constructor(props) {
  65. super(props);
  66. this.state = {
  67. /**
  68. * The Route rendered by this {@code AbstractApp}.
  69. *
  70. * @type {Route}
  71. */
  72. route: undefined,
  73. /**
  74. * The state of the »possible« async initialization of
  75. * the {@code AbstractApp}.
  76. */
  77. appAsyncInitialized: false,
  78. /**
  79. * The redux store used by this {@code AbstractApp}.
  80. *
  81. * @type {Store}
  82. */
  83. store: undefined
  84. };
  85. /**
  86. * Make the mobile {@code AbstractApp} wait until the
  87. * {@code AsyncStorage} implementation of {@code Storage} initializes
  88. * fully.
  89. *
  90. * @private
  91. * @see {@link #_initStorage}
  92. * @type {Promise}
  93. */
  94. this._init
  95. = this._initStorage()
  96. .catch(() => { /* AbstractApp should always initialize! */ })
  97. .then(() =>
  98. this.setState({
  99. route: undefined,
  100. store: this._maybeCreateStore(props)
  101. }));
  102. }
  103. /**
  104. * Init lib-jitsi-meet and create local participant when component is going
  105. * to be mounted.
  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 contructor 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. * Dispose lib-jitsi-meet and remove local participant when component is
  162. * going to be unmounted.
  163. *
  164. * @inheritdoc
  165. */
  166. componentWillUnmount() {
  167. const { dispatch } = this._getStore();
  168. dispatch(localParticipantLeft());
  169. dispatch(appWillUnmount(this));
  170. }
  171. /**
  172. * Gets a {@code Location} object from the window with information about the
  173. * current location of the document. Explicitly defined to allow extenders
  174. * to override because React Native does not usually have a location
  175. * property on its window unless debugging remotely in which case the
  176. * browser that is the remote debugger will provide a location property on
  177. * the window.
  178. *
  179. * @public
  180. * @returns {Location} A {@code Location} object with information about the
  181. * current location of the document.
  182. */
  183. getWindowLocation() {
  184. return undefined;
  185. }
  186. /**
  187. * Delays this {@code AbstractApp}'s startup until the {@code Storage}
  188. * implementation of {@code localStorage} initializes. While the
  189. * initialization is instantaneous on Web (with Web Storage API), it is
  190. * asynchronous on mobile/react-native.
  191. *
  192. * @private
  193. * @returns {Promise}
  194. */
  195. _initStorage() {
  196. const localStorageInitializing = window.localStorage._initializing;
  197. return (
  198. typeof localStorageInitializing === 'undefined'
  199. ? Promise.resolve()
  200. : localStorageInitializing);
  201. }
  202. /**
  203. * Implements React's {@link Component#render()}.
  204. *
  205. * @inheritdoc
  206. * @returns {ReactElement}
  207. */
  208. render() {
  209. const { appAsyncInitialized, route } = this.state;
  210. const component = (route && route.component) || BlankPage;
  211. if (appAsyncInitialized && component) {
  212. return (
  213. <I18nextProvider i18n = { i18next }>
  214. <Provider store = { this._getStore() }>
  215. <Fragment>
  216. { this._createElement(component) }
  217. <SoundCollection />
  218. <OverlayContainer />
  219. </Fragment>
  220. </Provider>
  221. </I18nextProvider>
  222. );
  223. }
  224. return null;
  225. }
  226. /**
  227. * Creates a {@link ReactElement} from the specified component, the
  228. * specified props and the props of this {@code AbstractApp} which are
  229. * suitable for propagation to the children of this {@code Component}.
  230. *
  231. * @param {Component} component - The component from which the
  232. * {@code ReactElement} is to be created.
  233. * @param {Object} props - The read-only React {@code Component} props with
  234. * which the {@code ReactElement} is to be initialized.
  235. * @returns {ReactElement}
  236. * @protected
  237. */
  238. _createElement(component, props) {
  239. /* eslint-disable no-unused-vars */
  240. const {
  241. // Don't propagate the dispatch and store props because they usually
  242. // come from react-redux and programmers don't really expect them to
  243. // be inherited but rather explicitly connected.
  244. dispatch, // eslint-disable-line react/prop-types
  245. store,
  246. // The following props were introduced to be consumed entirely by
  247. // AbstractApp:
  248. defaultURL,
  249. url,
  250. // The remaining props, if any, are considered suitable for
  251. // propagation to the children of this Component.
  252. ...thisProps
  253. } = this.props;
  254. /* eslint-enable no-unused-vars */
  255. return React.createElement(component, {
  256. ...thisProps,
  257. ...props
  258. });
  259. }
  260. /**
  261. * Initializes a new redux store instance suitable for use by this
  262. * {@code AbstractApp}.
  263. *
  264. * @private
  265. * @returns {Store} - A new redux store instance suitable for use by
  266. * this {@code AbstractApp}.
  267. */
  268. _createStore() {
  269. // Create combined reducer from all reducers in ReducerRegistry.
  270. const reducer = ReducerRegistry.combineReducers();
  271. // Apply all registered middleware from the MiddlewareRegistry and
  272. // additional 3rd party middleware:
  273. // - Thunk - allows us to dispatch async actions easily. For more info
  274. // @see https://github.com/gaearon/redux-thunk.
  275. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  276. // Try to enable Redux DevTools Chrome extension in order to make it
  277. // available for the purposes of facilitating development.
  278. let devToolsExtension;
  279. if (typeof window === 'object'
  280. && (devToolsExtension = window.devToolsExtension)) {
  281. middleware = compose(middleware, devToolsExtension());
  282. }
  283. return (
  284. createStore(
  285. reducer,
  286. PersistenceRegistry.getPersistedState(),
  287. middleware));
  288. }
  289. /**
  290. * Gets the default URL to be opened when this {@code App} mounts.
  291. *
  292. * @protected
  293. * @returns {string} The default URL to be opened when this {@code App}
  294. * mounts.
  295. */
  296. _getDefaultURL() {
  297. // If the execution environment provides a Location abstraction, then
  298. // this App at already at that location but it must be made aware of the
  299. // fact.
  300. const windowLocation = this.getWindowLocation();
  301. if (windowLocation) {
  302. const href = windowLocation.toString();
  303. if (href) {
  304. return href;
  305. }
  306. }
  307. return (
  308. this.props.defaultURL
  309. || this._getStore().getState()['features/base/settings']
  310. .serverURL
  311. || DEFAULT_URL);
  312. }
  313. /**
  314. * Gets the redux store used by this {@code AbstractApp}.
  315. *
  316. * @protected
  317. * @returns {Store} - The redux store used by this {@code AbstractApp}.
  318. */
  319. _getStore() {
  320. let store = this.state.store;
  321. if (typeof store === 'undefined') {
  322. store = this.props.store;
  323. }
  324. return store;
  325. }
  326. /**
  327. * Creates a redux store to be used by this {@code AbstractApp} if such as a
  328. * store is not defined by the consumer of this {@code AbstractApp} through
  329. * its read-only React {@code Component} props.
  330. *
  331. * @param {Object} props - The read-only React {@code Component} props that
  332. * will eventually be received by this {@code AbstractApp}.
  333. * @private
  334. * @returns {Store} - The redux store to be used by this
  335. * {@code AbstractApp}.
  336. */
  337. _maybeCreateStore(props) {
  338. // The application Jitsi Meet is architected with redux. However, I do
  339. // not want consumers of the App React Component to be forced into
  340. // dealing with redux. If the consumer did not provide an external redux
  341. // store, utilize an internal redux store.
  342. let store = props.store;
  343. if (typeof store === 'undefined') {
  344. store = this._createStore();
  345. // This is temporary workaround to be able to dispatch actions from
  346. // non-reactified parts of the code (conference.js for example).
  347. // Don't use in the react code!!!
  348. // FIXME: remove when the reactification is finished!
  349. if (typeof APP !== 'undefined') {
  350. APP.store = store;
  351. }
  352. }
  353. return store;
  354. }
  355. /**
  356. * Navigates to a specific Route.
  357. *
  358. * @param {Route} route - The Route to which to navigate.
  359. * @returns {Promise}
  360. */
  361. _navigate(route) {
  362. if (RouteRegistry.areRoutesEqual(this.state.route, route)) {
  363. return Promise.resolve();
  364. }
  365. let nextState = {
  366. route
  367. };
  368. // The Web App was using react-router so it utilized react-router's
  369. // onEnter. During the removal of react-router, modifications were
  370. // minimized by preserving the onEnter interface:
  371. // (1) Router would provide its nextState to the Route's onEnter. As the
  372. // role of Router is now this AbstractApp and we use redux, provide the
  373. // redux store instead.
  374. // (2) A replace function would be provided to the Route in case it
  375. // chose to redirect to another path.
  376. route && this._onRouteEnter(route, this._getStore(), pathname => {
  377. if (pathname) {
  378. this._openURL(pathname);
  379. // Do not proceed with the route because it chose to redirect to
  380. // another path.
  381. nextState = undefined;
  382. } else {
  383. nextState.route = undefined;
  384. }
  385. });
  386. // XXX React's setState is asynchronous which means that the value of
  387. // this.state.route above may not even be correct. If the check is
  388. // performed before setState completes, the app may not navigate to the
  389. // expected route. In order to mitigate the problem, _navigate was
  390. // changed to return a Promise.
  391. return new Promise(resolve => {
  392. if (nextState) {
  393. this.setState(nextState, resolve);
  394. } else {
  395. resolve();
  396. }
  397. });
  398. }
  399. /**
  400. * Notifies this {@code App} that a specific Route is about to be rendered.
  401. *
  402. * @param {Route} route - The Route that is about to be rendered.
  403. * @private
  404. * @returns {void}
  405. */
  406. _onRouteEnter(route, ...args) {
  407. // Notify the route that it is about to be entered.
  408. const { onEnter } = route;
  409. typeof onEnter === 'function' && onEnter(...args);
  410. }
  411. /**
  412. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  413. *
  414. * @param {Object|string} url - The URL to navigate this {@code AbstractApp}
  415. * to (i.e. the URL to open).
  416. * @protected
  417. * @returns {void}
  418. */
  419. _openURL(url) {
  420. this._getStore().dispatch(appNavigate(toURLString(url)));
  421. }
  422. }