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

reducer.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { combineReducers } from 'redux';
  2. import { ReducerRegistry } from '../redux';
  3. import {
  4. SET_AUDIO_MUTED,
  5. SET_CAMERA_FACING_MODE,
  6. SET_VIDEO_MUTED,
  7. TOGGLE_CAMERA_FACING_MODE
  8. } from './actionTypes';
  9. import { CAMERA_FACING_MODE } from './constants';
  10. /**
  11. * Media state object for local audio.
  12. *
  13. * @typedef {Object} AudioMediaState
  14. * @property {boolean} muted=false - Audio muted state.
  15. */
  16. /**
  17. * Initial state for local audio.
  18. *
  19. * @type {AudioMediaState}
  20. */
  21. const AUDIO_INITIAL_MEDIA_STATE = {
  22. muted: false
  23. };
  24. /**
  25. * Reducer for audio media state.
  26. *
  27. * @param {AudioMediaState} state - Media state of local audio.
  28. * @param {Object} action - Action object.
  29. * @param {string} action.type - Type of action.
  30. * @private
  31. * @returns {AudioMediaState}
  32. */
  33. function _audio(state = AUDIO_INITIAL_MEDIA_STATE, action) {
  34. switch (action.type) {
  35. case SET_AUDIO_MUTED:
  36. return {
  37. ...state,
  38. muted: action.muted
  39. };
  40. default:
  41. return state;
  42. }
  43. }
  44. /**
  45. * Media state object for local video.
  46. *
  47. * @typedef {Object} VideoMediaState
  48. * @property {CAMERA_FACING_MODE} facingMode='user' - Camera facing mode.
  49. * @property {boolean} muted=false - Video muted state.
  50. */
  51. /**
  52. * Initial state for video.
  53. *
  54. * @type {VideoMediaState}
  55. */
  56. const VIDEO_INITIAL_MEDIA_STATE = {
  57. facingMode: CAMERA_FACING_MODE.USER,
  58. muted: false
  59. };
  60. /**
  61. * Reducer for camera media state.
  62. *
  63. * @param {VideoMediaState} state - Media state of local video.
  64. * @param {Object} action - Action object.
  65. * @param {string} action.type - Type of action.
  66. * @private
  67. * @returns {VideoMediaState}
  68. */
  69. function _video(state = VIDEO_INITIAL_MEDIA_STATE, action) {
  70. switch (action.type) {
  71. case SET_CAMERA_FACING_MODE:
  72. return {
  73. ...state,
  74. facingMode: action.cameraFacingMode
  75. };
  76. case SET_VIDEO_MUTED:
  77. return {
  78. ...state,
  79. muted: action.muted
  80. };
  81. case TOGGLE_CAMERA_FACING_MODE: {
  82. let cameraFacingMode = state.facingMode;
  83. cameraFacingMode
  84. = cameraFacingMode === CAMERA_FACING_MODE.USER
  85. ? CAMERA_FACING_MODE.ENVIRONMENT
  86. : CAMERA_FACING_MODE.USER;
  87. return {
  88. ...state,
  89. facingMode: cameraFacingMode
  90. };
  91. }
  92. default:
  93. return state;
  94. }
  95. }
  96. /**
  97. * Listen for various actions related to media devices.
  98. *
  99. * @param {Object} state - State of media devices.
  100. * @param {Object} action - Action object.
  101. * @param {string} action.type - Type of action.
  102. * @param {Object} action.media - Information about media devices to be
  103. * modified.
  104. * @returns {Object}
  105. */
  106. ReducerRegistry.register('features/base/media', combineReducers({
  107. audio: _audio,
  108. video: _video
  109. }));