Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.js 3.8KB

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