Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { AnyAction } from 'redux';
  2. import { IStore } from '../../app/types';
  3. import { SET_CONFIG } from '../config/actionTypes';
  4. import { SET_NETWORK_INFO } from '../net-info/actionTypes';
  5. import { PARTICIPANT_LEFT } from '../participants/actionTypes';
  6. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  7. import JitsiMeetJS from './_';
  8. import { LIB_WILL_INIT } from './actionTypes';
  9. import { disposeLib, initLib } from './actions';
  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 SET_NETWORK_INFO:
  30. JitsiMeetJS.setNetworkInfo({
  31. isOnline: action.isOnline
  32. });
  33. break;
  34. case PARTICIPANT_LEFT:
  35. action.participant.local && store.dispatch(disposeLib());
  36. break;
  37. case SET_CONFIG:
  38. return _setConfig(store, next, action);
  39. }
  40. return next(action);
  41. });
  42. /**
  43. * Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
  44. * dispatched within a specific Redux store.
  45. *
  46. * @param {Store} store - The Redux store in which the specified action is being
  47. * dispatched.
  48. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  49. * specified action to the specified store.
  50. * @param {Action} action - The Redux action SET_CONFIG which is being
  51. * dispatched in the specified store.
  52. * @private
  53. * @returns {Object} The new state that is the result of the reduction of the
  54. * specified action.
  55. */
  56. function _setConfig({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  57. const { initialized } = getState()['features/base/lib-jitsi-meet'];
  58. // XXX Since the config is changing, the library lib-jitsi-meet must be
  59. // initialized again with the new config. Consequently, it may need to be
  60. // disposed of first.
  61. // TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
  62. // because lib-jitsi-meet does not implement such functionality.
  63. if (initialized) {
  64. dispatch(disposeLib());
  65. }
  66. // Let the new config into the Redux store (because initLib will read it
  67. // from there).
  68. const result = next(action);
  69. dispatch(initLib());
  70. return result;
  71. }
  72. /**
  73. * Attaches our custom error handlers to the window object.
  74. *
  75. * @returns {void}
  76. */
  77. function _setErrorHandlers() {
  78. // attaches global error handler, if there is already one, respect it
  79. if (JitsiMeetJS.getGlobalOnErrorHandler) {
  80. const oldOnErrorHandler = window.onerror;
  81. // TODO: Don't remove this ignore. The build fails on macOS and we don't know yet why.
  82. // @ts-ignore
  83. window.onerror = (message, source, lineno, colno, error) => { // eslint-disable-line max-params
  84. const errMsg = message || error?.message;
  85. const stack = error?.stack;
  86. JitsiMeetJS.getGlobalOnErrorHandler(errMsg, source, lineno, colno, stack);
  87. if (oldOnErrorHandler) {
  88. oldOnErrorHandler(message, source, lineno, colno, error);
  89. }
  90. };
  91. const oldOnUnhandledRejection = window.onunhandledrejection;
  92. window.onunhandledrejection = function(event) {
  93. let message = event.reason;
  94. let stack: string | undefined = 'n/a';
  95. if (event.reason instanceof Error) {
  96. ({ message, stack } = event.reason);
  97. }
  98. JitsiMeetJS.getGlobalOnErrorHandler(message, null, null, null, stack);
  99. if (oldOnUnhandledRejection) {
  100. // @ts-ignore
  101. oldOnUnhandledRejection(event);
  102. }
  103. };
  104. }
  105. }