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.2KB

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