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
 import { setTrackMuted, TRACK_ADDED } from '../tracks';
6
 import { setTrackMuted, TRACK_ADDED } from '../tracks';
7
 
7
 
8
 import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
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
  * Implements the entry point of the middleware of the feature base/media.
12
  * Implements the entry point of the middleware of the feature base/media.
104
     // fired before track gets to state.
104
     // fired before track gets to state.
105
     if (track.muted !== muted) {
105
     if (track.muted !== muted) {
106
         track.muted = muted;
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
 /* global APP */
1
 /* global APP */
2
 
2
 
3
 import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet';
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
 const logger = require('jitsi-meet-logger').getLogger(__filename);
6
 const logger = require('jitsi-meet-logger').getLogger(__filename);
7
 
7
 
159
 /**
159
 /**
160
  * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
160
  * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
161
  * the specified <tt>track</tt> is already in accord with the specified
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
  * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
167
  * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
165
  * unmute.
168
  * unmute.
166
  * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
169
  * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
167
  * <tt>true</tt>; otherwise, <tt>false</tt>.
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
     if (track.isMuted() === muted) {
177
     if (track.isMuted() === muted) {
172
         return Promise.resolve();
178
         return Promise.resolve();
173
     }
179
     }
174
 
180
 
175
     const f = muted ? 'mute' : 'unmute';
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
     const localTrack = _getLocalTrack(store, mediaType);
161
     const localTrack = _getLocalTrack(store, mediaType);
162
 
162
 
163
     if (localTrack) {
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