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.ts 3.9KB

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