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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 } from './actions';
  8. import { LIB_WILL_INIT } from './actionTypes';
  9. declare var APP: Object;
  10. /**
  11. * Middleware that captures PARTICIPANT_LEFT action for a local participant
  12. * (which signalizes that we finally left the app) and disposes lib-jitsi-meet.
  13. * Also captures SET_CONFIG action and disposes previous instance (if any) of
  14. * lib-jitsi-meet, and initializes a new one with new config.
  15. *
  16. * @param {Store} store - Redux store.
  17. * @private
  18. * @returns {Function}
  19. */
  20. MiddlewareRegistry.register(store => next => action => {
  21. switch (action.type) {
  22. case LIB_WILL_INIT:
  23. // Moved from conference.js init method. It appears the error handlers
  24. // are not used for mobile.
  25. if (typeof APP !== 'undefined') {
  26. _setErrorHandlers();
  27. }
  28. break;
  29. case PARTICIPANT_LEFT:
  30. action.participant.local && store.dispatch(disposeLib());
  31. break;
  32. case SET_CONFIG:
  33. return _setConfig(store, next, action);
  34. }
  35. return next(action);
  36. });
  37. /**
  38. * Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
  39. * dispatched within a specific Redux store.
  40. *
  41. * @param {Store} store - The Redux store in which the specified action is being
  42. * dispatched.
  43. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  44. * specified action to the specified store.
  45. * @param {Action} action - The Redux action SET_CONFIG which is being
  46. * dispatched in the specified store.
  47. * @private
  48. * @returns {Object} The new state that is the result of the reduction of the
  49. * specified action.
  50. */
  51. function _setConfig({ dispatch, getState }, next, action) {
  52. const { initialized } = getState()['features/base/lib-jitsi-meet'];
  53. // XXX Since the config is changing, the library lib-jitsi-meet must be
  54. // initialized again with the new config. Consequently, it may need to be
  55. // disposed of first.
  56. // TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
  57. // because lib-jitsi-meet does not implement such functionality.
  58. if (initialized) {
  59. dispatch(disposeLib());
  60. }
  61. // Let the new config into the Redux store (because initLib will read it
  62. // from there).
  63. const result = next(action);
  64. // FIXME Obviously, the following is bad design. However, I'm currently
  65. // introducing the features base/config and base/logging and I'm trying to
  66. // minimize the scope of the changes while I'm attempting to preserve
  67. // compatibility with the existing partially React-ified Web source code and
  68. // what was already executing on React Native. Additionally, I do not care
  69. // to load logging_config.js on React Native.
  70. dispatch(setLoggingConfig(window.loggingConfig));
  71. dispatch(initLib());
  72. return result;
  73. }
  74. /**
  75. * Attaches our custom error handlers to the window object.
  76. *
  77. * @returns {void}
  78. */
  79. function _setErrorHandlers() {
  80. // attaches global error handler, if there is already one, respect it
  81. if (JitsiMeetJS.getGlobalOnErrorHandler) {
  82. const oldOnErrorHandler = window.onerror;
  83. // eslint-disable-next-line max-params
  84. window.onerror = (message, source, lineno, colno, error) => {
  85. JitsiMeetJS.getGlobalOnErrorHandler(
  86. message, source, lineno, colno, error);
  87. if (oldOnErrorHandler) {
  88. oldOnErrorHandler(message, source, lineno, colno, error);
  89. }
  90. };
  91. const oldOnUnhandledRejection = window.onunhandledrejection;
  92. window.onunhandledrejection = function(event) {
  93. JitsiMeetJS.getGlobalOnErrorHandler(
  94. null, null, null, null, event.reason);
  95. if (oldOnUnhandledRejection) {
  96. oldOnUnhandledRejection(event);
  97. }
  98. };
  99. }
  100. }