Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

actions.js 4.2KB

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