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

actions.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { setRoom, setRoomURL } from '../base/conference';
  2. import { setConfig } from '../base/config';
  3. import { getDomain, setDomain } from '../base/connection';
  4. import { loadConfig } from '../base/lib-jitsi-meet';
  5. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes';
  6. import {
  7. _getRouteToRender,
  8. _parseURIString,
  9. init
  10. } from './functions';
  11. /**
  12. * Temporary solution. Should dispatch actions related to initial settings of
  13. * the app like setting log levels, reading the config parameters from query
  14. * string etc.
  15. *
  16. * @returns {Function}
  17. */
  18. export function appInit() {
  19. return (dispatch, getState) => init(getState());
  20. }
  21. /**
  22. * Triggers an in-app navigation to a different route. Allows navigation to be
  23. * abstracted between the mobile and web versions.
  24. *
  25. * @param {(string|undefined)} uri - The URI to which to navigate. It may be a
  26. * full URL with an http(s) scheme, a full or partial URI with the app-specific
  27. * sheme, or a mere room name.
  28. * @returns {Function}
  29. */
  30. export function appNavigate(uri) {
  31. return (dispatch, getState) => {
  32. const state = getState();
  33. const oldDomain = getDomain(state);
  34. const defaultURL = state['features/app'].app._getDefaultURL();
  35. let urlObject;
  36. // eslint-disable-next-line prefer-const
  37. let { domain, room } = _parseURIString(uri);
  38. // If the specified URI does not identify a domain, use the app's
  39. // default.
  40. if (typeof domain === 'undefined') {
  41. domain
  42. = _parseURIString(defaultURL)
  43. .domain;
  44. }
  45. if (room) {
  46. const splitUrl = uri.split(domain);
  47. const urlWithoutDomain = splitUrl[splitUrl.length - 1];
  48. urlObject = new URL(urlWithoutDomain, `https://${domain}`);
  49. }
  50. dispatch(setRoomURL(urlObject));
  51. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  52. // currently in a conference and ask her if she wants to close the
  53. // current conference and start a new one with the new room name or
  54. // domain.
  55. if (typeof domain === 'undefined' || oldDomain === domain) {
  56. dispatchSetRoomAndNavigate();
  57. } else if (oldDomain !== domain) {
  58. // Update domain without waiting for config to be loaded to prevent
  59. // race conditions when we will start to load config multiple times.
  60. dispatch(setDomain(domain));
  61. // If domain has changed, we need to load the config of the new
  62. // domain and set it, and only after that we can navigate to a
  63. // different route.
  64. loadConfig(`https://${domain}`)
  65. .then(
  66. config => configLoaded(/* err */ undefined, config),
  67. err => configLoaded(err, /* config */ undefined))
  68. .then(dispatchSetRoomAndNavigate);
  69. }
  70. /**
  71. * Notifies that an attempt to load the config(uration) of domain has
  72. * completed.
  73. *
  74. * @param {string|undefined} err - If the loading has failed, the error
  75. * detailing the cause of the failure.
  76. * @param {Object|undefined} config - If the loading has succeeded, the
  77. * loaded config(uration).
  78. * @returns {void}
  79. */
  80. function configLoaded(err, config) {
  81. if (err) {
  82. // XXX The failure could be, for example, because of a
  83. // certificate-related error. In which case the connection will
  84. // fail later in Strophe anyway even if we use the default
  85. // config here.
  86. // The function loadConfig will log the err.
  87. return;
  88. }
  89. dispatch(setConfig(config));
  90. }
  91. /**
  92. * Dispatches _setRoomAndNavigate in the Redux store.
  93. *
  94. * @returns {void}
  95. */
  96. function dispatchSetRoomAndNavigate() {
  97. // If both domain and room vars became undefined, that means we're
  98. // actually dealing with just room name and not with URL.
  99. dispatch(
  100. _setRoomAndNavigate(
  101. typeof room === 'undefined' && typeof domain === 'undefined'
  102. ? uri
  103. : room));
  104. }
  105. };
  106. }
  107. /**
  108. * Signals that a specific App will mount (in the terms of React).
  109. *
  110. * @param {App} app - The App which will mount.
  111. * @returns {{
  112. * type: APP_WILL_MOUNT,
  113. * app: App
  114. * }}
  115. */
  116. export function appWillMount(app) {
  117. return {
  118. type: APP_WILL_MOUNT,
  119. app
  120. };
  121. }
  122. /**
  123. * Signals that a specific App will unmount (in the terms of React).
  124. *
  125. * @param {App} app - The App which will unmount.
  126. * @returns {{
  127. * type: APP_WILL_UNMOUNT,
  128. * app: App
  129. * }}
  130. */
  131. export function appWillUnmount(app) {
  132. return {
  133. type: APP_WILL_UNMOUNT,
  134. app
  135. };
  136. }
  137. /**
  138. * Navigates to a route in accord with a specific Redux state.
  139. *
  140. * @param {Object} state - The Redux state which determines/identifies the route
  141. * to navigate to.
  142. * @private
  143. * @returns {void}
  144. */
  145. function _navigate(state) {
  146. const app = state['features/app'].app;
  147. const routeToRender = _getRouteToRender(state);
  148. app._navigate(routeToRender);
  149. }
  150. /**
  151. * Sets room and navigates to new route if needed.
  152. *
  153. * @param {string} newRoom - New room name.
  154. * @private
  155. * @returns {Function}
  156. */
  157. function _setRoomAndNavigate(newRoom) {
  158. return (dispatch, getState) => {
  159. dispatch(setRoom(newRoom));
  160. _navigate(getState());
  161. };
  162. }