Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* @flow */
  2. import type { Dispatch } from 'redux';
  3. import JitsiMeetJS from './_';
  4. import {
  5. LIB_DID_DISPOSE,
  6. LIB_DID_INIT,
  7. LIB_INIT_ERROR,
  8. LIB_INIT_PROMISE_CREATED,
  9. LIB_WILL_DISPOSE,
  10. LIB_WILL_INIT,
  11. SET_WEBRTC_READY
  12. } from './actionTypes';
  13. import { isAnalyticsEnabled } from './functions';
  14. declare var APP: Object;
  15. /**
  16. * Disposes (of) lib-jitsi-meet.
  17. *
  18. * @returns {Function}
  19. */
  20. export function disposeLib() {
  21. return (dispatch: Dispatch<*>) => {
  22. dispatch({ type: LIB_WILL_DISPOSE });
  23. // TODO Currently, lib-jitsi-meet doesn't have the functionality to
  24. // dispose itself.
  25. dispatch({ type: LIB_DID_DISPOSE });
  26. };
  27. }
  28. /**
  29. * Initializes lib-jitsi-meet (i.e. {@link invokes JitsiMeetJS.init()}) with the
  30. * current config(uration).
  31. *
  32. * @returns {Function}
  33. */
  34. export function initLib() {
  35. return (dispatch: Dispatch<*>, getState: Function): Promise<void> => {
  36. const config = getState()['features/base/config'];
  37. if (!config) {
  38. throw new Error('Cannot init lib-jitsi-meet without config');
  39. }
  40. dispatch({ type: LIB_WILL_INIT });
  41. const initPromise = JitsiMeetJS.init({
  42. enableAnalyticsLogging: isAnalyticsEnabled(getState),
  43. ...config
  44. });
  45. dispatch({
  46. type: LIB_INIT_PROMISE_CREATED,
  47. initPromise
  48. });
  49. return (
  50. initPromise
  51. .then(() => dispatch({ type: LIB_DID_INIT }))
  52. .catch(error => {
  53. // TODO: See the comment in the connect action in
  54. // base/connection/actions.web.js.
  55. if (typeof APP === 'undefined') {
  56. dispatch(libInitError(error));
  57. }
  58. // TODO Handle LIB_INIT_ERROR error somewhere instead.
  59. console.error('lib-jitsi-meet failed to init:', error);
  60. throw error;
  61. }));
  62. };
  63. }
  64. /**
  65. * Notifies about a specific error raised by {@link JitsiMeetJS.init()}.
  66. *
  67. * @param {Error} error - The Error raised by JitsiMeetJS.init().
  68. * @returns {{
  69. * type: LIB_INIT_ERROR,
  70. * error: Error
  71. * }}
  72. */
  73. export function libInitError(error: Error) {
  74. return {
  75. type: LIB_INIT_ERROR,
  76. error
  77. };
  78. }
  79. /**
  80. * Sets the indicator which determines whether WebRTC is ready. In execution
  81. * environments in which WebRTC is supported via a known plugin such
  82. * as Temasys WebRTC may start not ready and then become ready. Of course, there
  83. * are execution enviroments such as old Mozilla Firefox versions or
  84. * certains Microsoft Edge versions in which WebRTC is not supported at all.
  85. *
  86. * @param {boolean|Promise} webRTCReady - The indicator which determines
  87. * whether WebRTC is ready. If a Promise is specified, its resolution will be
  88. * awaited.
  89. * @returns {Function}
  90. */
  91. export function setWebRTCReady(webRTCReady: boolean | Promise<*>) {
  92. return (dispatch: Function, getState: Function) => {
  93. if (getState()['features/base/lib-jitsi-meet'].webRTCReady
  94. !== webRTCReady) {
  95. dispatch({
  96. type: SET_WEBRTC_READY,
  97. webRTCReady
  98. });
  99. // If the specified webRTCReady is a thenable (i.e. a Promise), then
  100. // await its resolution.
  101. switch (typeof webRTCReady) {
  102. case 'function':
  103. case 'object': {
  104. const { then } = webRTCReady;
  105. if (typeof then === 'function') {
  106. const onFulfilled = value => {
  107. // Is the app still interested in the specified
  108. // webRTCReady?
  109. if (getState()['features/base/lib-jitsi-meet']
  110. .webRTCReady
  111. === webRTCReady) {
  112. dispatch(setWebRTCReady(value));
  113. }
  114. };
  115. then.call(
  116. webRTCReady,
  117. /* onFulfilled */ () => onFulfilled(true),
  118. /* onRejected*/ () => onFulfilled(false));
  119. }
  120. break;
  121. }
  122. }
  123. }
  124. };
  125. }