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.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // @flow
  2. import { setAudioOnly } from '../audio-only';
  3. import { getLocalParticipant, participantUpdated } from '../participants';
  4. import { MiddlewareRegistry } from '../redux';
  5. import { SETTINGS_UPDATED } from './actionTypes';
  6. import { handleCallIntegrationChange } from './functions';
  7. /**
  8. * The middleware of the feature base/settings. Distributes changes to the state
  9. * of base/settings to the states of other features computed from the state of
  10. * base/settings.
  11. *
  12. * @param {Store} store - The redux store.
  13. * @returns {Function}
  14. */
  15. MiddlewareRegistry.register(store => next => action => {
  16. const result = next(action);
  17. switch (action.type) {
  18. case SETTINGS_UPDATED:
  19. _maybeHandleCallIntegrationChange(action);
  20. _maybeSetAudioOnly(store, action);
  21. _updateLocalParticipant(store, action);
  22. }
  23. return result;
  24. });
  25. /**
  26. * Maps the settings field names to participant names where they don't match.
  27. * Currently there is only one such field, but may be extended in the future.
  28. *
  29. * @private
  30. * @param {string} settingsField - The name of the settings field to map.
  31. * @returns {string}
  32. */
  33. function _mapSettingsFieldToParticipant(settingsField) {
  34. switch (settingsField) {
  35. case 'displayName':
  36. return 'name';
  37. }
  38. return settingsField;
  39. }
  40. /**
  41. * Updates {@code startAudioOnly} flag if it's updated in the settings.
  42. *
  43. * @param {Object} action - The redux action.
  44. * @private
  45. * @returns {void}
  46. */
  47. function _maybeHandleCallIntegrationChange({ settings: { disableCallIntegration } }) {
  48. if (typeof disableCallIntegration === 'boolean') {
  49. handleCallIntegrationChange(disableCallIntegration);
  50. }
  51. }
  52. /**
  53. * Updates {@code startAudioOnly} flag if it's updated in the settings.
  54. *
  55. * @param {Store} store - The redux store.
  56. * @param {Object} action - The redux action.
  57. * @private
  58. * @returns {void}
  59. */
  60. function _maybeSetAudioOnly(
  61. { dispatch },
  62. { settings: { startAudioOnly } }) {
  63. if (typeof startAudioOnly === 'boolean') {
  64. dispatch(setAudioOnly(startAudioOnly, true));
  65. }
  66. }
  67. /**
  68. * Updates the local participant according to settings changes.
  69. *
  70. * @param {Store} store - The redux store.
  71. * @param {Object} action - The dispatched action.
  72. * @private
  73. * @returns {void}
  74. */
  75. function _updateLocalParticipant({ dispatch, getState }, action) {
  76. const { settings } = action;
  77. const localParticipant = getLocalParticipant(getState());
  78. const newLocalParticipant = {
  79. ...localParticipant
  80. };
  81. for (const key in settings) {
  82. if (settings.hasOwnProperty(key)) {
  83. newLocalParticipant[_mapSettingsFieldToParticipant(key)]
  84. = settings[key];
  85. }
  86. }
  87. dispatch(participantUpdated(newLocalParticipant));
  88. }