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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. enableAnalyticsLogging: isAnalyticsEnabled(getState),
  47. ...config
  48. })
  49. .then(() => dispatch({ type: LIB_DID_INIT }))
  50. .catch(error => {
  51. dispatch(libInitError(error));
  52. // TODO Handle LIB_INIT_ERROR error somewhere instead.
  53. console.error('lib-jitsi-meet failed to init:', error);
  54. throw error;
  55. }));
  56. };
  57. }
  58. /**
  59. * Notifies about a specific error raised by {@link JitsiMeetJS.init()}.
  60. *
  61. * @param {Error} error - The Error raised by JitsiMeetJS.init().
  62. * @returns {{
  63. * type: LIB_INIT_ERROR,
  64. * error: Error
  65. * }}
  66. */
  67. export function libInitError(error: Error) {
  68. return {
  69. type: LIB_INIT_ERROR,
  70. error
  71. };
  72. }
  73. /**
  74. * Sets the indicator which determines whether WebRTC is ready. In execution
  75. * environments in which WebRTC is supported via a known plugin such
  76. * as Temasys WebRTC may start not ready and then become ready. Of course, there
  77. * are execution enviroments such as old Mozilla Firefox versions or
  78. * certains Microsoft Edge versions in which WebRTC is not supported at all.
  79. *
  80. * @param {boolean|Promise} webRTCReady - The indicator which determines
  81. * whether WebRTC is ready. If a Promise is specified, its resolution will be
  82. * awaited.
  83. * @returns {Function}
  84. */
  85. export function setWebRTCReady(webRTCReady: boolean | Promise<*>) {
  86. return (dispatch: Dispatch<*>, getState: Function) => {
  87. if (getState()['features/base/lib-jitsi-meet'].webRTCReady
  88. !== webRTCReady) {
  89. dispatch({
  90. type: SET_WEBRTC_READY,
  91. webRTCReady
  92. });
  93. // If the specified webRTCReady is a thenable (i.e. a Promise), then
  94. // await its resolution.
  95. switch (typeof webRTCReady) {
  96. case 'function':
  97. case 'object': {
  98. const { then } = webRTCReady;
  99. if (typeof then === 'function') {
  100. const onFulfilled = value => {
  101. // Is the app still interested in the specified
  102. // webRTCReady?
  103. if (getState()['features/base/lib-jitsi-meet']
  104. .webRTCReady
  105. === webRTCReady) {
  106. dispatch(setWebRTCReady(value));
  107. }
  108. };
  109. then.call(
  110. webRTCReady,
  111. /* onFulfilled */ () => onFulfilled(true),
  112. /* onRejected*/ () => onFulfilled(false));
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. };
  119. }