您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.any.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { IStore } from '../app/types';
  2. import { getCurrentConference } from '../base/conference/functions';
  3. import { PARTICIPANT_LEFT, PIN_PARTICIPANT } from '../base/participants/actionTypes';
  4. import { pinParticipant } from '../base/participants/actions';
  5. import { getParticipantById, getPinnedParticipant } from '../base/participants/functions';
  6. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  7. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  8. import { SET_DOCUMENT_EDITING_STATUS } from '../etherpad/actionTypes';
  9. import { isStageFilmstripEnabled } from '../filmstrip/functions';
  10. import { isFollowMeActive } from '../follow-me/functions';
  11. import { SET_TILE_VIEW } from './actionTypes';
  12. import { setTileView } from './actions';
  13. import { getAutoPinSetting, updateAutoPinnedParticipant } from './functions';
  14. import './subscriber';
  15. let previousTileViewEnabled: boolean | undefined;
  16. /**
  17. * Middleware which intercepts actions and updates tile view related state.
  18. *
  19. * @param {Store} store - The redux store.
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. // we want to extract the leaving participant and check its type before actually the participant being removed.
  24. let shouldUpdateAutoPin = false;
  25. switch (action.type) {
  26. case PARTICIPANT_LEFT: {
  27. if (!getAutoPinSetting() || isFollowMeActive(store)) {
  28. break;
  29. }
  30. shouldUpdateAutoPin = Boolean(getParticipantById(store.getState(), action.participant.id)?.fakeParticipant);
  31. break;
  32. }
  33. }
  34. const result = next(action);
  35. switch (action.type) {
  36. // Actions that temporarily clear the user preferred state of tile view,
  37. // then re-set it when needed.
  38. case PIN_PARTICIPANT: {
  39. const pinnedParticipant = action.participant?.id;
  40. if (pinnedParticipant) {
  41. _storeTileViewStateAndClear(store);
  42. } else {
  43. _restoreTileViewState(store);
  44. }
  45. break;
  46. }
  47. case SET_DOCUMENT_EDITING_STATUS:
  48. if (action.editing) {
  49. _storeTileViewStateAndClear(store);
  50. } else {
  51. _restoreTileViewState(store);
  52. }
  53. break;
  54. // Things to update when tile view state changes
  55. case SET_TILE_VIEW: {
  56. const state = store.getState();
  57. const stageFilmstrip = isStageFilmstripEnabled(state);
  58. if (action.enabled && !stageFilmstrip && getPinnedParticipant(state)) {
  59. store.dispatch(pinParticipant(null));
  60. }
  61. break;
  62. }
  63. }
  64. if (shouldUpdateAutoPin) {
  65. const screenShares = store.getState()['features/video-layout'].remoteScreenShares || [];
  66. updateAutoPinnedParticipant(screenShares, store);
  67. }
  68. return result;
  69. });
  70. /**
  71. * Set up state change listener to perform maintenance tasks when the conference
  72. * is left or failed.
  73. */
  74. StateListenerRegistry.register(
  75. state => getCurrentConference(state),
  76. (conference, { dispatch }, previousConference) => {
  77. if (conference !== previousConference) {
  78. // conference changed, left or failed...
  79. // Clear tile view state.
  80. dispatch(setTileView());
  81. }
  82. });
  83. /**
  84. * Restores tile view state, if it wasn't updated since then.
  85. *
  86. * @param {Object} store - The Redux Store.
  87. * @returns {void}
  88. */
  89. function _restoreTileViewState({ dispatch, getState }: IStore) {
  90. const { tileViewEnabled } = getState()['features/video-layout'];
  91. if (tileViewEnabled === undefined && previousTileViewEnabled !== undefined) {
  92. dispatch(setTileView(previousTileViewEnabled));
  93. }
  94. previousTileViewEnabled = undefined;
  95. }
  96. /**
  97. * Stores the current tile view state and clears it.
  98. *
  99. * @param {Object} store - The Redux Store.
  100. * @returns {void}
  101. */
  102. function _storeTileViewStateAndClear({ dispatch, getState }: IStore) {
  103. const { tileViewEnabled } = getState()['features/video-layout'];
  104. if (tileViewEnabled !== undefined) {
  105. previousTileViewEnabled = tileViewEnabled;
  106. dispatch(setTileView(undefined));
  107. }
  108. }