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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { AnyAction } from 'redux';
  2. import { IStore } from '../../app/types';
  3. import { getFeatureFlag } from '../flags/functions';
  4. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  5. import { updateSettings } from '../settings/actions';
  6. import { OVERWRITE_CONFIG, SET_CONFIG } from './actionTypes';
  7. import { updateConfig } from './actions';
  8. import { IConfig } from './configType';
  9. /**
  10. * The middleware of the feature {@code base/config}.
  11. *
  12. * @param {Store} store - The redux store.
  13. * @private
  14. * @returns {Function}
  15. */
  16. MiddlewareRegistry.register(store => next => action => {
  17. switch (action.type) {
  18. case SET_CONFIG:
  19. return _setConfig(store, next, action);
  20. case OVERWRITE_CONFIG:
  21. return _updateSettings(store, next, action);
  22. }
  23. return next(action);
  24. });
  25. /**
  26. * Notifies the feature {@code base/config} that the {@link SET_CONFIG} redux
  27. * action is being {@code dispatch}ed in a specific redux store.
  28. *
  29. * @param {Store} store - The redux store in which the specified {@code action}
  30. * is being dispatched.
  31. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  32. * specified {@code action} in the specified {@code store}.
  33. * @param {Action} action - The redux action which is being {@code dispatch}ed
  34. * in the specified {@code store}.
  35. * @private
  36. * @returns {*} The return value of {@code next(action)}.
  37. */
  38. function _setConfig({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  39. // The reducer is doing some alterations to the config passed in the action,
  40. // so make sure it's the final state by waiting for the action to be
  41. // reduced.
  42. const result = next(action);
  43. const state = getState();
  44. // Update the config with user defined settings.
  45. const settings = state['features/base/settings'];
  46. const config: IConfig = {};
  47. if (typeof settings.disableP2P !== 'undefined') {
  48. config.p2p = { enabled: !settings.disableP2P };
  49. }
  50. const resolutionFlag = getFeatureFlag(state, 'resolution');
  51. if (typeof resolutionFlag !== 'undefined') {
  52. config.resolution = resolutionFlag;
  53. }
  54. if (action.config.doNotFlipLocalVideo === true) {
  55. dispatch(updateSettings({
  56. localFlipX: false
  57. }));
  58. }
  59. if (action.config.disableSelfView !== undefined) {
  60. dispatch(updateSettings({
  61. disableSelfView: action.config.disableSelfView
  62. }));
  63. }
  64. if (action.config.filmstrip?.stageFilmstripParticipants !== undefined) {
  65. dispatch(updateSettings({
  66. maxStageParticipants: action.config.filmstrip.stageFilmstripParticipants
  67. }));
  68. }
  69. dispatch(updateConfig(config));
  70. // FIXME On Web we rely on the global 'config' variable which gets altered
  71. // multiple times, before it makes it to the reducer. At some point it may
  72. // not be the global variable which is being modified anymore due to
  73. // different merge methods being used along the way. The global variable
  74. // must be synchronized with the final state resolved by the reducer.
  75. if (typeof window.config !== 'undefined') {
  76. window.config = state['features/base/config'];
  77. }
  78. return result;
  79. }
  80. /**
  81. * Updates settings based on some config values.
  82. *
  83. * @param {Store} store - The redux store in which the specified {@code action}
  84. * is being dispatched.
  85. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  86. * specified {@code action} in the specified {@code store}.
  87. * @param {Action} action - The redux action which is being {@code dispatch}ed
  88. * in the specified {@code store}.
  89. * @private
  90. * @returns {*} The return value of {@code next(action)}.
  91. */
  92. function _updateSettings({ dispatch }: IStore, next: Function, action: AnyAction) {
  93. const { config: { doNotFlipLocalVideo } } = action;
  94. if (doNotFlipLocalVideo === true) {
  95. dispatch(updateSettings({
  96. localFlipX: false
  97. }));
  98. }
  99. return next(action);
  100. }