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

reducer.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { PARTICIPANT_ID_CHANGED } from '../participants';
  2. import { ReducerRegistry } from '../redux';
  3. import {
  4. TRACK_ADDED,
  5. TRACK_REMOVED,
  6. TRACK_UPDATED
  7. } from './actionTypes';
  8. /**
  9. * @typedef {Object} Track
  10. * @property {(JitsiLocalTrack|JitsiRemoteTrack)} jitsiTrack - JitsiTrack
  11. * instance.
  12. * @property {boolean} local=false - If track is local.
  13. * @property {MEDIA_TYPE} mediaType=false - Media type of track.
  14. * @property {boolean} mirror=false - The indicator which determines whether the
  15. * display/rendering of the track should be mirrored. It only makes sense in the
  16. * context of video (at least at the time of this writing).
  17. * @property {boolean} muted=false - If track is muted.
  18. * @property {(string|undefined)} participantId - ID of participant whom this
  19. * track belongs to.
  20. * @property {boolean} videoStarted=false - If video track has already started
  21. * to play.
  22. * @property {(VIDEO_TYPE|undefined)} videoType - Type of video track if any.
  23. */
  24. /**
  25. * Reducer function for a single track.
  26. *
  27. * @param {Track|undefined} state - Track to be modified.
  28. * @param {Object} action - Action object.
  29. * @param {string} action.type - Type of action.
  30. * @param {string} action.newValue - New participant ID value (in this
  31. * particular case).
  32. * @param {string} action.oldValue - Old participant ID value (in this
  33. * particular case).
  34. * @param {Track} action.track - Information about track to be changed.
  35. * @param {Participant} action.participant - Information about participant.
  36. * @returns {Track|undefined}
  37. */
  38. function track(state, action) {
  39. switch (action.type) {
  40. case PARTICIPANT_ID_CHANGED:
  41. if (state.participantId === action.oldValue) {
  42. return {
  43. ...state,
  44. participantId: action.newValue
  45. };
  46. }
  47. break;
  48. case TRACK_UPDATED: {
  49. const t = action.track;
  50. if (state.jitsiTrack === t.jitsiTrack) {
  51. // Make sure that there's an actual update in order to reduce the
  52. // risk of unnecessary React Component renders.
  53. for (const p in t) {
  54. if (state[p] !== t[p]) {
  55. // There's an actual update.
  56. return {
  57. ...state,
  58. ...t
  59. };
  60. }
  61. }
  62. }
  63. break;
  64. }
  65. }
  66. return state;
  67. }
  68. /**
  69. * Listen for actions that mutate (e.g. add, remove) local and remote tracks.
  70. */
  71. ReducerRegistry.register('features/base/tracks', (state = [], action) => {
  72. switch (action.type) {
  73. case PARTICIPANT_ID_CHANGED:
  74. case TRACK_UPDATED:
  75. return state.map(t => track(t, action));
  76. case TRACK_ADDED:
  77. return [
  78. ...state,
  79. action.track
  80. ];
  81. case TRACK_REMOVED:
  82. return state.filter(t => t.jitsiTrack !== action.track.jitsiTrack);
  83. default:
  84. return state;
  85. }
  86. });