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

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