您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {
  2. SET_AUDIO_INPUT_DEVICE,
  3. SET_AUDIO_OUTPUT_DEVICE,
  4. SET_VIDEO_INPUT_DEVICE,
  5. UPDATE_DEVICE_LIST
  6. } from './actionTypes';
  7. import { ReducerRegistry } from '../redux';
  8. const DEFAULT_STATE = {
  9. audioInput: [],
  10. audioOutput: [],
  11. videoInput: []
  12. };
  13. /**
  14. * Listen for actions which changes the state of known and used devices.
  15. *
  16. * @param {Object} state - The Redux state of the feature features/base/devices.
  17. * @param {Object} action - Action object.
  18. * @param {string} action.type - Type of action.
  19. * @param {Array<MediaDeviceInfo>} action.devices - All available audio and
  20. * video devices.
  21. * @returns {Object}
  22. */
  23. ReducerRegistry.register(
  24. 'features/base/devices',
  25. (state = DEFAULT_STATE, action) => {
  26. switch (action.type) {
  27. case UPDATE_DEVICE_LIST: {
  28. const deviceList = _groupDevicesByKind(action.devices);
  29. return {
  30. ...deviceList
  31. };
  32. }
  33. // TODO: Changing of current audio and video device id is currently
  34. // handled outside of react/redux. Fall through to default logic for
  35. // now.
  36. case SET_AUDIO_INPUT_DEVICE:
  37. case SET_VIDEO_INPUT_DEVICE:
  38. case SET_AUDIO_OUTPUT_DEVICE:
  39. default:
  40. return state;
  41. }
  42. });
  43. /**
  44. * Converts an array of media devices into an object organized by device kind.
  45. *
  46. * @param {Array<MediaDeviceInfo>} devices - Available media devices.
  47. * @private
  48. * @returns {Object} An object with the media devices split by type. The keys
  49. * are device type and the values are arrays with devices matching the device
  50. * type.
  51. */
  52. function _groupDevicesByKind(devices) {
  53. return {
  54. audioInput: devices.filter(device => device.kind === 'audioinput'),
  55. audioOutput: devices.filter(device => device.kind === 'audiooutput'),
  56. videoInput: devices.filter(device => device.kind === 'videoinput')
  57. };
  58. }