您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { CONNECTION_ESTABLISHED } from '../connection';
  2. import {
  3. getLocalParticipant,
  4. getParticipantById,
  5. PIN_PARTICIPANT
  6. } from '../participants';
  7. import { MiddlewareRegistry } from '../redux';
  8. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  9. import { createConference } from './actions';
  10. import {
  11. _addLocalTracksToConference,
  12. _handleParticipantError,
  13. _removeLocalTracksFromConference
  14. } from './functions';
  15. /**
  16. * Implements the middleware of the feature base/conference.
  17. *
  18. * @param {Store} store - Redux store.
  19. * @returns {Function}
  20. */
  21. MiddlewareRegistry.register(store => next => action => {
  22. switch (action.type) {
  23. case CONNECTION_ESTABLISHED:
  24. return _connectionEstablished(store, next, action);
  25. case PIN_PARTICIPANT:
  26. return _pinParticipant(store, next, action);
  27. case TRACK_ADDED:
  28. case TRACK_REMOVED:
  29. return _trackAddedOrRemoved(store, next, action);
  30. }
  31. return next(action);
  32. });
  33. /**
  34. * Notifies the feature base/conference that the action CONNECTION_ESTABLISHED
  35. * is being dispatched within a specific Redux store.
  36. *
  37. * @param {Store} store - The Redux store in which the specified action is being
  38. * dispatched.
  39. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  40. * specified action to the specified store.
  41. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED which is
  42. * being dispatched in the specified store.
  43. * @private
  44. * @returns {Object} The new state that is the result of the reduction of the
  45. * specified action.
  46. */
  47. function _connectionEstablished(store, next, action) {
  48. const result = next(action);
  49. store.dispatch(createConference());
  50. return result;
  51. }
  52. /**
  53. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  54. * dispatched within a specific Redux store. Pins the specified remote
  55. * participant in the associated conference, ignores the local participant.
  56. *
  57. * @param {Store} store - The Redux store in which the specified action is being
  58. * dispatched.
  59. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  60. * specified action to the specified store.
  61. * @param {Action} action - The Redux action PIN_PARTICIPANT which is being
  62. * dispatched in the specified store.
  63. * @private
  64. * @returns {Object} The new state that is the result of the reduction of the
  65. * specified action.
  66. */
  67. function _pinParticipant(store, next, action) {
  68. const state = store.getState();
  69. const participants = state['features/base/participants'];
  70. const id = action.participant.id;
  71. const participantById = getParticipantById(participants, id);
  72. let pin;
  73. // The following condition prevents signaling to pin local participant. The
  74. // logic is:
  75. // - If we have an ID, we check if the participant identified by that ID is
  76. // local.
  77. // - If we don't have an ID (i.e. no participant identified by an ID), we
  78. // check for local participant. If she's currently pinned, then this
  79. // action will unpin her and that's why we won't signal here too.
  80. if (participantById) {
  81. pin = !participantById.local;
  82. } else {
  83. const localParticipant = getLocalParticipant(participants);
  84. pin = !localParticipant || !localParticipant.pinned;
  85. }
  86. if (pin) {
  87. const conference = state['features/base/conference'].conference;
  88. try {
  89. conference.pinParticipant(id);
  90. } catch (err) {
  91. _handleParticipantError(err);
  92. }
  93. }
  94. return next(action);
  95. }
  96. /**
  97. * Synchronizes local tracks from state with local tracks in JitsiConference
  98. * instance.
  99. *
  100. * @param {Store} store - Redux store.
  101. * @param {Object} action - Action object.
  102. * @private
  103. * @returns {Promise}
  104. */
  105. function _syncConferenceLocalTracksWithState(store, action) {
  106. const state = store.getState()['features/base/conference'];
  107. const conference = state.conference;
  108. let promise;
  109. // XXX The conference may already be in the process of being left, that's
  110. // why we should not add/remove local tracks to such conference.
  111. if (conference && conference !== state.leaving) {
  112. const track = action.track.jitsiTrack;
  113. if (action.type === TRACK_ADDED) {
  114. promise = _addLocalTracksToConference(conference, [ track ]);
  115. } else {
  116. promise = _removeLocalTracksFromConference(conference, [ track ]);
  117. }
  118. }
  119. return promise || Promise.resolve();
  120. }
  121. /**
  122. * Notifies the feature base/conference that the action TRACK_ADDED
  123. * or TRACK_REMOVED is being dispatched within a specific Redux store.
  124. *
  125. * @param {Store} store - The Redux store in which the specified action is being
  126. * dispatched.
  127. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  128. * specified action to the specified store.
  129. * @param {Action} action - The Redux action TRACK_ADDED or TRACK_REMOVED which
  130. * is being dispatched in the specified store.
  131. * @private
  132. * @returns {Object} The new state that is the result of the reduction of the
  133. * specified action.
  134. */
  135. function _trackAddedOrRemoved(store, next, action) {
  136. const track = action.track;
  137. if (track && track.local) {
  138. return (
  139. _syncConferenceLocalTracksWithState(store, action)
  140. .then(() => next(action)));
  141. }
  142. return next(action);
  143. }