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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { AnyAction } from 'redux';
  2. import { PARTICIPANT_ID_CHANGED } from '../participants/actionTypes';
  3. import ReducerRegistry from '../redux/ReducerRegistry';
  4. import { set } from '../redux/functions';
  5. import {
  6. SET_NO_SRC_DATA_NOTIFICATION_UID,
  7. TRACK_ADDED,
  8. TRACK_CREATE_CANCELED,
  9. TRACK_CREATE_ERROR,
  10. TRACK_NO_DATA_FROM_SOURCE,
  11. TRACK_OWNER_CHANGED,
  12. TRACK_REMOVED,
  13. TRACK_UPDATED,
  14. TRACK_WILL_CREATE
  15. } from './actionTypes';
  16. import { ITrack } from './types';
  17. /**
  18. * Reducer function for a single track.
  19. *
  20. * @param {Track|undefined} state - Track to be modified.
  21. * @param {Object} action - Action object.
  22. * @param {string} action.type - Type of action.
  23. * @param {string} action.name - Name of last media event.
  24. * @param {string} action.newValue - New participant ID value (in this
  25. * particular case).
  26. * @param {string} action.oldValue - Old participant ID value (in this
  27. * particular case).
  28. * @param {Track} action.track - Information about track to be changed.
  29. * @param {Participant} action.participant - Information about participant.
  30. * @returns {Track|undefined}
  31. */
  32. function track(state: ITrack, action: AnyAction) {
  33. switch (action.type) {
  34. case PARTICIPANT_ID_CHANGED:
  35. if (state.participantId === action.oldValue) {
  36. return {
  37. ...state,
  38. participantId: action.newValue
  39. };
  40. }
  41. break;
  42. case TRACK_OWNER_CHANGED: {
  43. const t = action.track;
  44. if (state.jitsiTrack === t.jitsiTrack) {
  45. return {
  46. ...state,
  47. participantId: t.participantId
  48. };
  49. }
  50. break;
  51. }
  52. case TRACK_UPDATED: {
  53. const t = action.track;
  54. if (state.jitsiTrack === t.jitsiTrack) {
  55. // Make sure that there's an actual update in order to reduce the
  56. // risk of unnecessary React Component renders.
  57. for (const p in t) {
  58. // @ts-ignore
  59. if (state[p] !== t[p]) {
  60. // There's an actual update.
  61. return {
  62. ...state,
  63. ...t
  64. };
  65. }
  66. }
  67. }
  68. break;
  69. }
  70. case TRACK_NO_DATA_FROM_SOURCE: {
  71. const t = action.track;
  72. if (state.jitsiTrack === t.jitsiTrack) {
  73. const isReceivingData = t.jitsiTrack.isReceivingData();
  74. if (state.isReceivingData !== isReceivingData) {
  75. return {
  76. ...state,
  77. isReceivingData
  78. };
  79. }
  80. }
  81. break;
  82. }
  83. }
  84. return state;
  85. }
  86. export type ITracksState = ITrack[];
  87. /**
  88. * Listen for actions that mutate (e.g. Add, remove) local and remote tracks.
  89. */
  90. ReducerRegistry.register<ITracksState>('features/base/tracks', (state = [], action): ITracksState => {
  91. switch (action.type) {
  92. case PARTICIPANT_ID_CHANGED:
  93. case TRACK_NO_DATA_FROM_SOURCE:
  94. case TRACK_OWNER_CHANGED:
  95. case TRACK_UPDATED:
  96. return state.map((t: ITrack) => track(t, action));
  97. case TRACK_ADDED: {
  98. let withoutTrackStub = state;
  99. if (action.track.local) {
  100. withoutTrackStub
  101. = state.filter(
  102. (t: ITrack) => !t.local || t.mediaType !== action.track.mediaType);
  103. }
  104. return [ ...withoutTrackStub, action.track ];
  105. }
  106. case TRACK_CREATE_CANCELED:
  107. case TRACK_CREATE_ERROR: {
  108. return state.filter((t: ITrack) => !t.local || t.mediaType !== action.trackType);
  109. }
  110. case TRACK_REMOVED:
  111. return state.filter((t: ITrack) => t.jitsiTrack !== action.track.jitsiTrack);
  112. case TRACK_WILL_CREATE:
  113. return [ ...state, action.track ];
  114. default:
  115. return state;
  116. }
  117. });
  118. export interface INoSrcDataState {
  119. noSrcDataNotificationUid?: string | number;
  120. }
  121. /**
  122. * Listen for actions that mutate the no-src-data state, like the current notification id.
  123. */
  124. ReducerRegistry.register<INoSrcDataState>('features/base/no-src-data', (state = {}, action): INoSrcDataState => {
  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. });