You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.js 4.1KB

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