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.ts 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { disposeLib, initLib } from './actions';
  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 SET_NETWORK_INFO:
  22. JitsiMeetJS.setNetworkInfo({
  23. isOnline: action.isOnline
  24. });
  25. break;
  26. case PARTICIPANT_LEFT:
  27. action.participant.local && store.dispatch(disposeLib());
  28. break;
  29. case SET_CONFIG:
  30. return _setConfig(store, next, action);
  31. }
  32. return next(action);
  33. });
  34. /**
  35. * Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
  36. * dispatched within a specific Redux store.
  37. *
  38. * @param {Store} store - The Redux store in which the specified action is being
  39. * dispatched.
  40. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  41. * specified action to the specified store.
  42. * @param {Action} action - The Redux action SET_CONFIG which is being
  43. * dispatched in the specified store.
  44. * @private
  45. * @returns {Object} The new state that is the result of the reduction of the
  46. * specified action.
  47. */
  48. function _setConfig({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  49. const { initialized } = getState()['features/base/lib-jitsi-meet'];
  50. // XXX Since the config is changing, the library lib-jitsi-meet must be
  51. // initialized again with the new config. Consequently, it may need to be
  52. // disposed of first.
  53. // TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
  54. // because lib-jitsi-meet does not implement such functionality.
  55. if (initialized) {
  56. dispatch(disposeLib());
  57. }
  58. // Let the new config into the Redux store (because initLib will read it
  59. // from there).
  60. const result = next(action);
  61. dispatch(initLib());
  62. return result;
  63. }