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.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import ReducerRegistry from '../redux/ReducerRegistry';
  2. import {
  3. ADD_PENDING_DEVICE_REQUEST,
  4. DEVICE_PERMISSIONS_CHANGED,
  5. REMOVE_PENDING_DEVICE_REQUESTS,
  6. SET_AUDIO_INPUT_DEVICE,
  7. SET_VIDEO_INPUT_DEVICE,
  8. UPDATE_DEVICE_LIST
  9. } from './actionTypes';
  10. import { groupDevicesByKind } from './functions';
  11. import logger from './logger';
  12. const DEFAULT_STATE: IDevicesState = {
  13. availableDevices: {
  14. audioInput: [],
  15. audioOutput: [],
  16. videoInput: []
  17. },
  18. pendingRequests: [],
  19. permissions: {
  20. audio: false,
  21. video: false
  22. }
  23. };
  24. export interface IDevicesState {
  25. availableDevices: {
  26. audioInput?: MediaDeviceInfo[];
  27. audioOutput?: MediaDeviceInfo[];
  28. videoInput?: MediaDeviceInfo[];
  29. };
  30. pendingRequests: Object[];
  31. permissions: {
  32. audio: boolean;
  33. video: boolean;
  34. };
  35. }
  36. /**
  37. * Listen for actions which changes the state of known and used devices.
  38. *
  39. * @param {IDevicesState} state - The Redux state of the feature features/base/devices.
  40. * @param {Object} action - Action object.
  41. * @param {string} action.type - Type of action.
  42. * @param {Array<MediaDeviceInfo>} action.devices - All available audio and
  43. * video devices.
  44. * @returns {Object}
  45. */
  46. ReducerRegistry.register<IDevicesState>(
  47. 'features/base/devices',
  48. (state = DEFAULT_STATE, action): IDevicesState => {
  49. switch (action.type) {
  50. case UPDATE_DEVICE_LIST: {
  51. const deviceList = groupDevicesByKind(action.devices);
  52. return {
  53. ...state,
  54. availableDevices: deviceList
  55. };
  56. }
  57. case ADD_PENDING_DEVICE_REQUEST:
  58. return {
  59. ...state,
  60. pendingRequests: [
  61. ...state.pendingRequests,
  62. action.request
  63. ]
  64. };
  65. case REMOVE_PENDING_DEVICE_REQUESTS:
  66. return {
  67. ...state,
  68. pendingRequests: [ ]
  69. };
  70. // TODO: Changing of current audio and video device id is currently handled outside of react/redux.
  71. case SET_AUDIO_INPUT_DEVICE: {
  72. logger.debug(`set audio input device: ${action.deviceId}`);
  73. return state;
  74. }
  75. case SET_VIDEO_INPUT_DEVICE: {
  76. logger.debug(`set video input device: ${action.deviceId}`);
  77. return state;
  78. }
  79. case DEVICE_PERMISSIONS_CHANGED: {
  80. return {
  81. ...state,
  82. permissions: action.permissions
  83. };
  84. }
  85. default:
  86. return state;
  87. }
  88. });