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 3.9KB

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