Browse Source

ref(tracks): remove duplication in mute/unmute

master
paweldomas 8 years ago
parent
commit
40c9f583fa

+ 2
- 13
react/features/base/media/middleware.js View File

@@ -6,7 +6,7 @@ import { MiddlewareRegistry } from '../redux';
6 6
 import { setTrackMuted, TRACK_ADDED } from '../tracks';
7 7
 
8 8
 import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
9
-import { CAMERA_FACING_MODE, MEDIA_TYPE } from './constants';
9
+import { CAMERA_FACING_MODE } from './constants';
10 10
 
11 11
 /**
12 12
  * Implements the entry point of the middleware of the feature base/media.
@@ -104,17 +104,6 @@ function _syncTrackMutedState(store, track) {
104 104
     // fired before track gets to state.
105 105
     if (track.muted !== muted) {
106 106
         track.muted = muted;
107
-        setTrackMuted(track.jitsiTrack, muted)
108
-            .catch(error => {
109
-                console.error(`setTrackMuted(${muted}) failed`, error);
110
-
111
-                const setMuted
112
-                    = track.mediaType === MEDIA_TYPE.AUDIO
113
-                        ? setAudioMuted
114
-                        : setVideoMuted;
115
-
116
-                // Failed to sync muted state - dispatch rollback action
117
-                store.dispatch(setMuted(!muted));
118
-            });
107
+        setTrackMuted(track.jitsiTrack, muted, store);
119 108
     }
120 109
 }

+ 20
- 5
react/features/base/tracks/functions.js View File

@@ -1,7 +1,7 @@
1 1
 /* global APP */
2 2
 
3 3
 import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet';
4
-import { MEDIA_TYPE } from '../media';
4
+import { MEDIA_TYPE, setAudioMuted, setVideoMuted } from '../media';
5 5
 
6 6
 const logger = require('jitsi-meet-logger').getLogger(__filename);
7 7
 
@@ -159,20 +159,35 @@ export function getTracksByMediaType(tracks, mediaType) {
159 159
 /**
160 160
  * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
161 161
  * the specified <tt>track</tt> is already in accord with the specified
162
- * <tt>muted</tt> value, then does nothing.
162
+ * <tt>muted</tt> value, then does nothing. In case mute/unmute operation fails
163
+ * (JitsiLocalTrack Promise is rejected) a rollback action will be dispatched on
164
+ * the given store. For example if the mute operation fails then the 'unmute'
165
+ * action will be dispatched to rollback to the previous state in base/media.
163 166
  *
164 167
  * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
165 168
  * unmute.
166 169
  * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
167 170
  * <tt>true</tt>; otherwise, <tt>false</tt>.
168
- * @returns {Promise}
171
+ * @param {Store} store - The redux store in the context of which the function
172
+ * is to execute and which will be used to dispatch the rollback action in case
173
+ * mute/unmute fails.
174
+* @returns {Promise}
169 175
  */
170
-export function setTrackMuted(track, muted) {
176
+export function setTrackMuted(track, muted, { dispatch }) {
171 177
     if (track.isMuted() === muted) {
172 178
         return Promise.resolve();
173 179
     }
174 180
 
175 181
     const f = muted ? 'mute' : 'unmute';
176 182
 
177
-    return track[f]();
183
+    return track[f]().catch(error => {
184
+        console.error(`set track ${f} failed`, error);
185
+
186
+        const setMuted
187
+            = track.mediaType === MEDIA_TYPE.AUDIO
188
+                ? setAudioMuted
189
+                : setVideoMuted;
190
+
191
+        dispatch(setMuted(!muted));
192
+    });
178 193
 }

+ 1
- 11
react/features/base/tracks/middleware.js View File

@@ -161,17 +161,7 @@ function _setMuted(store, action, mediaType: MEDIA_TYPE) {
161 161
     const localTrack = _getLocalTrack(store, mediaType);
162 162
 
163 163
     if (localTrack) {
164
-        setTrackMuted(localTrack.jitsiTrack, action.muted)
165
-            .catch(error => {
166
-                console.error(`setTrackMuted(${action.muted}) failed`, error);
167
-
168
-                const setMuted
169
-                    = mediaType === MEDIA_TYPE.AUDIO
170
-                        ? setAudioMuted : setVideoMuted;
171
-
172
-                // Failed to modify muted state - dispatch rollback action
173
-                store.dispatch(setMuted(!action.muted));
174
-            });
164
+        setTrackMuted(localTrack.jitsiTrack, action.muted, store);
175 165
     }
176 166
 }
177 167
 

Loading…
Cancel
Save