您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 4.6KB

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