Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

reducer.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // @flow
  2. import { ReducerRegistry, set } from '../redux';
  3. import {
  4. DOMINANT_SPEAKER_CHANGED,
  5. PARTICIPANT_ID_CHANGED,
  6. PARTICIPANT_JOINED,
  7. PARTICIPANT_LEFT,
  8. PARTICIPANT_UPDATED,
  9. PIN_PARTICIPANT,
  10. SET_LOADABLE_AVATAR_URL
  11. } from './actionTypes';
  12. import { LOCAL_PARTICIPANT_DEFAULT_ID, PARTICIPANT_ROLE } from './constants';
  13. /**
  14. * Participant object.
  15. * @typedef {Object} Participant
  16. * @property {string} id - Participant ID.
  17. * @property {string} name - Participant name.
  18. * @property {string} avatar - Path to participant avatar if any.
  19. * @property {string} role - Participant role.
  20. * @property {boolean} local - If true, participant is local.
  21. * @property {boolean} pinned - If true, participant is currently a
  22. * "PINNED_ENDPOINT".
  23. * @property {boolean} dominantSpeaker - If this participant is the dominant
  24. * speaker in the (associated) conference, {@code true}; otherwise,
  25. * {@code false}.
  26. * @property {string} email - Participant email.
  27. */
  28. declare var APP: Object;
  29. /**
  30. * The participant properties which cannot be updated through
  31. * {@link PARTICIPANT_UPDATED}. They either identify the participant or can only
  32. * be modified through property-dedicated actions.
  33. *
  34. * @type {string[]}
  35. */
  36. const PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE = [
  37. // The following properties identify the participant:
  38. 'conference',
  39. 'id',
  40. 'local',
  41. // The following properties can only be modified through property-dedicated
  42. // actions:
  43. 'dominantSpeaker',
  44. 'pinned'
  45. ];
  46. /**
  47. * Listen for actions which add, remove, or update the set of participants in
  48. * the conference.
  49. *
  50. * @param {Participant[]} state - List of participants to be modified.
  51. * @param {Object} action - Action object.
  52. * @param {string} action.type - Type of action.
  53. * @param {Participant} action.participant - Information about participant to be
  54. * added/removed/modified.
  55. * @returns {Participant[]}
  56. */
  57. ReducerRegistry.register('features/base/participants', (state = [], action) => {
  58. switch (action.type) {
  59. case SET_LOADABLE_AVATAR_URL:
  60. case DOMINANT_SPEAKER_CHANGED:
  61. case PARTICIPANT_ID_CHANGED:
  62. case PARTICIPANT_UPDATED:
  63. case PIN_PARTICIPANT:
  64. return state.map(p => _participant(p, action));
  65. case PARTICIPANT_JOINED:
  66. // dev inspect
  67. const participant_new = _participantJoined(action)
  68. clog("PARTICIPANT_JOINED RFN dev inspect",{state,action,participant_new})
  69. var ret = [ ...state, participant_new ]
  70. clog("PARTICIPANT_JOINED RFN dev inspect2",ret)
  71. ret = new Proxy(ret,gen_proxy())
  72. // const ret = new Proxy([ ...state, participant_new ],rprox)
  73. return ret;
  74. // return [ ...state, _participantJoined(action) ];
  75. case PARTICIPANT_LEFT: {
  76. // XXX A remote participant is uniquely identified by their id in a
  77. // specific JitsiConference instance. The local participant is uniquely
  78. // identified by the very fact that there is only one local participant
  79. // (and the fact that the local participant "joins" at the beginning of
  80. // the app and "leaves" at the end of the app).
  81. const { conference, id } = action.participant;
  82. return state.filter(p =>
  83. !(
  84. p.id === id
  85. // XXX Do not allow collisions in the IDs of the local
  86. // participant and a remote participant cause the removal of
  87. // the local participant when the remote participant's
  88. // removal is requested.
  89. && p.conference === conference
  90. && (conference || p.local)));
  91. }
  92. }
  93. return state;
  94. });
  95. /**
  96. * Reducer function for a single participant.
  97. *
  98. * @param {Participant|undefined} state - Participant to be modified.
  99. * @param {Object} action - Action object.
  100. * @param {string} action.type - Type of action.
  101. * @param {Participant} action.participant - Information about participant to be
  102. * added/modified.
  103. * @param {JitsiConference} action.conference - Conference instance.
  104. * @private
  105. * @returns {Participant}
  106. */
  107. function _participant(state: Object = {}, action) {
  108. switch (action.type) {
  109. case DOMINANT_SPEAKER_CHANGED:
  110. // Only one dominant speaker is allowed.
  111. return (
  112. set(state, 'dominantSpeaker', state.id === action.participant.id));
  113. case PARTICIPANT_ID_CHANGED: {
  114. // A participant is identified by an id-conference pair. Only the local
  115. // participant is with an undefined conference.
  116. const { conference } = action;
  117. if (state.id === action.oldValue
  118. && state.conference === conference
  119. && (conference || state.local)) {
  120. return {
  121. ...state,
  122. id: action.newValue
  123. };
  124. }
  125. break;
  126. }
  127. case SET_LOADABLE_AVATAR_URL:
  128. case PARTICIPANT_UPDATED: {
  129. const { participant } = action; // eslint-disable-line no-shadow
  130. let { id } = participant;
  131. const { local } = participant;
  132. if (!id && local) {
  133. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  134. }
  135. if (state.id === id) {
  136. const newState = { ...state };
  137. for (const key in participant) {
  138. if (participant.hasOwnProperty(key)
  139. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  140. === -1) {
  141. newState[key] = participant[key];
  142. }
  143. }
  144. return newState;
  145. }
  146. break;
  147. }
  148. case PIN_PARTICIPANT:
  149. // Currently, only one pinned participant is allowed.
  150. return set(state, 'pinned', state.id === action.participant.id);
  151. }
  152. return state;
  153. }
  154. /**
  155. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  156. * feature base/participants.
  157. *
  158. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  159. * to reduce.
  160. * @private
  161. * @returns {Object} The new participant derived from the payload of the
  162. * specified {@code action} to be added into the redux state of the feature
  163. * base/participants after the reduction of the specified
  164. * {@code action}.
  165. */
  166. function _participantJoined({ participant }) {
  167. const {
  168. avatarID,
  169. avatarURL,
  170. botType,
  171. connectionStatus,
  172. dominantSpeaker,
  173. email,
  174. isFakeParticipant,
  175. isJigasi,
  176. loadableAvatarUrl,
  177. local,
  178. name,
  179. pinned,
  180. presence,
  181. role
  182. } = participant;
  183. let { conference, id } = participant;
  184. if (local) {
  185. // conference
  186. //
  187. // XXX The local participant is not identified in association with a
  188. // JitsiConference because it is identified by the very fact that it is
  189. // the local participant.
  190. conference = undefined;
  191. // id
  192. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  193. }
  194. return {
  195. avatarID,
  196. avatarURL,
  197. botType,
  198. conference,
  199. connectionStatus,
  200. dominantSpeaker: dominantSpeaker || false,
  201. email,
  202. id,
  203. isFakeParticipant,
  204. isJigasi,
  205. loadableAvatarUrl,
  206. local: local || false,
  207. name,
  208. pinned: pinned || false,
  209. presence,
  210. role: role || PARTICIPANT_ROLE.NONE
  211. };
  212. }