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.

reducer.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { ReducerRegistry } from '../base/redux';
  2. import {
  3. RESET_DESKTOP_SOURCES,
  4. UPDATE_DESKTOP_SOURCES
  5. } from './actionTypes';
  6. const defaultState = {
  7. screen: [],
  8. window: []
  9. };
  10. /**
  11. * Listen for actions that mutate the known available DesktopCapturerSources.
  12. *
  13. * @param {Object[]} state - Current state.
  14. * @param {Object} action - Action object.
  15. * @param {string} action.type - Type of action.
  16. * @param {Array} action.sources - DesktopCapturerSources.
  17. * @returns {Object}
  18. */
  19. ReducerRegistry.register(
  20. 'features/desktop-picker/sources',
  21. (state = defaultState, action) => {
  22. switch (action.type) {
  23. case RESET_DESKTOP_SOURCES:
  24. return { ...defaultState };
  25. case UPDATE_DESKTOP_SOURCES:
  26. return seperateSourcesByType(action.sources);
  27. default:
  28. return state;
  29. }
  30. });
  31. /**
  32. * Converts an array of DesktopCapturerSources to an object with types
  33. * for keys and values being an array with sources of the key's type.
  34. *
  35. * @param {Array} sources - DesktopCapturerSources.
  36. * @returns {Object} An object with the sources split into seperate arrays
  37. * based on source type.
  38. * @private
  39. */
  40. function seperateSourcesByType(sources = []) {
  41. const sourcesByType = {
  42. screen: [],
  43. window: []
  44. };
  45. sources.forEach(source => {
  46. const sourceIdParts = source.id.split(':');
  47. const sourceType = sourceIdParts[0];
  48. if (sourcesByType[sourceType]) {
  49. sourcesByType[sourceType].push(source);
  50. }
  51. });
  52. return sourcesByType;
  53. }