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.any.js 3.6KB

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