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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* @flow */
  2. import { SET_CONFIG } from '../config';
  3. import { setLoggingConfig } from '../logging';
  4. import { SET_NETWORK_INFO } from '../net-info';
  5. import { PARTICIPANT_LEFT } from '../participants';
  6. import { MiddlewareRegistry } from '../redux';
  7. import JitsiMeetJS from './_';
  8. import { LIB_WILL_INIT } from './actionTypes';
  9. import { disposeLib, initLib } from './actions';
  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 SET_NETWORK_INFO:
  31. JitsiMeetJS.setNetworkInfo({
  32. isOnline: action.isOnline
  33. });
  34. break;
  35. case PARTICIPANT_LEFT:
  36. action.participant.local && store.dispatch(disposeLib());
  37. break;
  38. case SET_CONFIG:
  39. return _setConfig(store, next, action);
  40. }
  41. return next(action);
  42. });
  43. /**
  44. * Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
  45. * dispatched within a specific Redux store.
  46. *
  47. * @param {Store} store - The Redux store in which the specified action is being
  48. * dispatched.
  49. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  50. * specified action to the specified store.
  51. * @param {Action} action - The Redux action SET_CONFIG which is being
  52. * dispatched in the specified store.
  53. * @private
  54. * @returns {Object} The new state that is the result of the reduction of the
  55. * specified action.
  56. */
  57. function _setConfig({ dispatch, getState }, next, action) {
  58. const { initialized } = getState()['features/base/lib-jitsi-meet'];
  59. // XXX Since the config is changing, the library lib-jitsi-meet must be
  60. // initialized again with the new config. Consequently, it may need to be
  61. // disposed of first.
  62. // TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
  63. // because lib-jitsi-meet does not implement such functionality.
  64. if (initialized) {
  65. dispatch(disposeLib());
  66. }
  67. // Let the new config into the Redux store (because initLib will read it
  68. // from there).
  69. const result = next(action);
  70. // FIXME Obviously, the following is bad design. However, I'm currently
  71. // introducing the features base/config and base/logging and I'm trying to
  72. // minimize the scope of the changes while I'm attempting to preserve
  73. // compatibility with the existing partially React-ified Web source code and
  74. // what was already executing on React Native. Additionally, I do not care
  75. // to load logging_config.js on React Native.
  76. dispatch(setLoggingConfig(window.loggingConfig));
  77. dispatch(initLib());
  78. return result;
  79. }
  80. /**
  81. * Attaches our custom error handlers to the window object.
  82. *
  83. * @returns {void}
  84. */
  85. function _setErrorHandlers() {
  86. // attaches global error handler, if there is already one, respect it
  87. if (JitsiMeetJS.getGlobalOnErrorHandler) {
  88. const oldOnErrorHandler = window.onerror;
  89. // eslint-disable-next-line max-params
  90. window.onerror = (message, source, lineno, colno, error) => {
  91. const errMsg = message || (error && error.message);
  92. const stack = error && error.stack;
  93. JitsiMeetJS.getGlobalOnErrorHandler(errMsg, source, lineno, colno, stack);
  94. if (oldOnErrorHandler) {
  95. oldOnErrorHandler(message, source, lineno, colno, error);
  96. }
  97. };
  98. const oldOnUnhandledRejection = window.onunhandledrejection;
  99. window.onunhandledrejection = function(event) {
  100. let message = event.reason;
  101. let stack = 'n/a';
  102. if (event.reason instanceof Error) {
  103. ({ message, stack } = event.reason);
  104. }
  105. JitsiMeetJS.getGlobalOnErrorHandler(message, null, null, null, stack);
  106. if (oldOnUnhandledRejection) {
  107. oldOnUnhandledRejection(event);
  108. }
  109. };
  110. }
  111. }