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.

middleware.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* @flow */
  2. import { SET_CONFIG } from '../config';
  3. import { setLoggingConfig } from '../logging';
  4. import { PARTICIPANT_LEFT } from '../participants';
  5. import { MiddlewareRegistry } from '../redux';
  6. import JitsiMeetJS from './_';
  7. import { disposeLib, initLib, setWebRTCReady } from './actions';
  8. import { LIB_DID_INIT, LIB_INIT_ERROR, LIB_WILL_INIT } from './actionTypes';
  9. import { WEBRTC_NOT_READY, WEBRTC_NOT_SUPPORTED } from './constants';
  10. declare var APP: Object;
  11. /**
  12. * Middleware that captures PARTICIPANT_LEFT action for a local participant
  13. * (which signalizes that we finally left the app) and disposes lib-jitsi-meet.
  14. * Also captures SET_CONFIG action and disposes previous instance (if any) of
  15. * lib-jitsi-meet, and initializes a new one with new config.
  16. *
  17. * @param {Store} store - Redux store.
  18. * @private
  19. * @returns {Function}
  20. */
  21. MiddlewareRegistry.register(store => next => action => {
  22. switch (action.type) {
  23. case LIB_WILL_INIT:
  24. // Moved from conference.js init method. It appears the error handlers
  25. // are not used for mobile.
  26. if (typeof APP !== 'undefined') {
  27. _setErrorHandlers();
  28. }
  29. break;
  30. case LIB_DID_INIT:
  31. // FIXME: The web version doesn't need this action during initialization
  32. // because it is still using the old logic from conference.js. We still
  33. // have to reactify the old logic from conference.js and then maybe
  34. // we'll need this action for web too.
  35. if (typeof APP === 'undefined') {
  36. store.dispatch(setWebRTCReady(true));
  37. }
  38. break;
  39. case LIB_INIT_ERROR:
  40. return _libInitError(store, next, action);
  41. case PARTICIPANT_LEFT:
  42. action.participant.local && store.dispatch(disposeLib());
  43. break;
  44. case SET_CONFIG:
  45. return _setConfig(store, next, action);
  46. }
  47. return next(action);
  48. });
  49. /**
  50. * Notifies the feature base/lib-jitsi-meet that the action LIB_INIT_ERROR is
  51. * being dispatched within a specific Redux store.
  52. *
  53. * @param {Store} store - The Redux store in which the specified action is being
  54. * dispatched.
  55. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  56. * specified action to the specified store.
  57. * @param {Action} action - The Redux action LIB_INIT_ERROR which is being
  58. * dispatched in the specified store.
  59. * @private
  60. * @returns {Object} The new state that is the result of the reduction of the
  61. * specified action.
  62. */
  63. function _libInitError(store, next, action) {
  64. const nextState = next(action);
  65. const { error } = action;
  66. if (error) {
  67. let webRTCReady;
  68. switch (error.name) {
  69. case WEBRTC_NOT_READY:
  70. webRTCReady = error.webRTCReadyPromise;
  71. break;
  72. case WEBRTC_NOT_SUPPORTED:
  73. webRTCReady = false;
  74. break;
  75. }
  76. typeof webRTCReady === 'undefined'
  77. || store.dispatch(setWebRTCReady(webRTCReady));
  78. }
  79. return nextState;
  80. }
  81. /**
  82. * Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
  83. * dispatched within a specific Redux store.
  84. *
  85. * @param {Store} store - The Redux store in which the specified action is being
  86. * dispatched.
  87. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  88. * specified action to the specified store.
  89. * @param {Action} action - The Redux action SET_CONFIG which is being
  90. * dispatched in the specified store.
  91. * @private
  92. * @returns {Object} The new state that is the result of the reduction of the
  93. * specified action.
  94. */
  95. function _setConfig({ dispatch, getState }, next, action) {
  96. const { initialized } = getState()['features/base/lib-jitsi-meet'];
  97. // XXX Since the config is changing, the library lib-jitsi-meet must be
  98. // initialized again with the new config. Consequently, it may need to be
  99. // disposed of first.
  100. // TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
  101. // because lib-jitsi-meet does not implement such functionality.
  102. if (initialized) {
  103. dispatch(disposeLib());
  104. }
  105. // Let the new config into the Redux store (because initLib will read it
  106. // from there).
  107. const result = next(action);
  108. // FIXME Obviously, the following is bad design. However, I'm currently
  109. // introducing the features base/config and base/logging and I'm trying to
  110. // minimize the scope of the changes while I'm attempting to preserve
  111. // compatibility with the existing partially React-ified Web source code and
  112. // what was already executing on React Native. Additionally, I do not care
  113. // to load logging_config.js on React Native.
  114. dispatch(setLoggingConfig(window.loggingConfig));
  115. dispatch(initLib());
  116. return result;
  117. }
  118. /**
  119. * Attaches our custom error handlers to the window object.
  120. *
  121. * @returns {void}
  122. */
  123. function _setErrorHandlers() {
  124. // attaches global error handler, if there is already one, respect it
  125. if (JitsiMeetJS.getGlobalOnErrorHandler) {
  126. const oldOnErrorHandler = window.onerror;
  127. // eslint-disable-next-line max-params
  128. window.onerror = (message, source, lineno, colno, error) => {
  129. JitsiMeetJS.getGlobalOnErrorHandler(
  130. message, source, lineno, colno, error);
  131. if (oldOnErrorHandler) {
  132. oldOnErrorHandler(message, source, lineno, colno, error);
  133. }
  134. };
  135. const oldOnUnhandledRejection = window.onunhandledrejection;
  136. window.onunhandledrejection = function(event) {
  137. JitsiMeetJS.getGlobalOnErrorHandler(
  138. null, null, null, null, event.reason);
  139. if (oldOnUnhandledRejection) {
  140. oldOnUnhandledRejection(event);
  141. }
  142. };
  143. }
  144. }