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 3.7KB

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