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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_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 && error.name === WEBRTC_NOT_SUPPORTED) {
  67. store.dispatch(setWebRTCReady(false));
  68. }
  69. return nextState;
  70. }
  71. /**
  72. * Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
  73. * dispatched within a specific Redux store.
  74. *
  75. * @param {Store} store - The Redux store in which the specified action is being
  76. * dispatched.
  77. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  78. * specified action to the specified store.
  79. * @param {Action} action - The Redux action SET_CONFIG which is being
  80. * dispatched in the specified store.
  81. * @private
  82. * @returns {Object} The new state that is the result of the reduction of the
  83. * specified action.
  84. */
  85. function _setConfig({ dispatch, getState }, next, action) {
  86. const { initialized } = getState()['features/base/lib-jitsi-meet'];
  87. // XXX Since the config is changing, the library lib-jitsi-meet must be
  88. // initialized again with the new config. Consequently, it may need to be
  89. // disposed of first.
  90. // TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
  91. // because lib-jitsi-meet does not implement such functionality.
  92. if (initialized) {
  93. dispatch(disposeLib());
  94. }
  95. // Let the new config into the Redux store (because initLib will read it
  96. // from there).
  97. const result = next(action);
  98. // FIXME Obviously, the following is bad design. However, I'm currently
  99. // introducing the features base/config and base/logging and I'm trying to
  100. // minimize the scope of the changes while I'm attempting to preserve
  101. // compatibility with the existing partially React-ified Web source code and
  102. // what was already executing on React Native. Additionally, I do not care
  103. // to load logging_config.js on React Native.
  104. dispatch(setLoggingConfig(window.loggingConfig));
  105. dispatch(initLib());
  106. return result;
  107. }
  108. /**
  109. * Attaches our custom error handlers to the window object.
  110. *
  111. * @returns {void}
  112. */
  113. function _setErrorHandlers() {
  114. // attaches global error handler, if there is already one, respect it
  115. if (JitsiMeetJS.getGlobalOnErrorHandler) {
  116. const oldOnErrorHandler = window.onerror;
  117. // eslint-disable-next-line max-params
  118. window.onerror = (message, source, lineno, colno, error) => {
  119. JitsiMeetJS.getGlobalOnErrorHandler(
  120. message, source, lineno, colno, error);
  121. if (oldOnErrorHandler) {
  122. oldOnErrorHandler(message, source, lineno, colno, error);
  123. }
  124. };
  125. const oldOnUnhandledRejection = window.onunhandledrejection;
  126. window.onunhandledrejection = function(event) {
  127. JitsiMeetJS.getGlobalOnErrorHandler(
  128. null, null, null, null, event.reason);
  129. if (oldOnUnhandledRejection) {
  130. oldOnUnhandledRejection(event);
  131. }
  132. };
  133. }
  134. }