Browse Source

Convert a function to an asynchronous redux action creator

master
Lyubo Marinov 8 years ago
parent
commit
dcc6ce025f

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

@@ -93,8 +93,8 @@ function _setRoom({ dispatch, getState }, next, action) {
93 93
  * @private
94 94
  * @returns {void}
95 95
  */
96
-function _syncTrackMutedState(store, track) {
97
-    const state = store.getState()['features/base/media'];
96
+function _syncTrackMutedState({ dispatch, getState }, track) {
97
+    const state = getState()['features/base/media'];
98 98
     const muted = state[track.mediaType].muted;
99 99
 
100 100
     // XXX If muted state of track when it was added is different from our media
@@ -104,6 +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, store);
107
+        dispatch(setTrackMuted(track.jitsiTrack, muted));
108 108
     }
109 109
 }

+ 33
- 0
react/features/base/tracks/actions.js View File

@@ -344,6 +344,39 @@ function _getLocalTracksToChange(currentTracks, newTracks) {
344 344
     };
345 345
 }
346 346
 
347
+/**
348
+ * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
349
+ * the specified <tt>track</tt> is already in accord with the specified
350
+ * <tt>muted</tt> value, then does nothing. In case the actual muting/unmuting
351
+ * fails, a rollback action will be dispatched to undo the muting/unmuting.
352
+ *
353
+ * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
354
+ * unmute.
355
+ * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
356
+ * <tt>true</tt>; otherwise, <tt>false</tt>.
357
+ * @returns {Function}
358
+ */
359
+export function setTrackMuted(track, muted) {
360
+    return dispatch => {
361
+        if (track.isMuted() === muted) {
362
+            return Promise.resolve();
363
+        }
364
+
365
+        const f = muted ? 'mute' : 'unmute';
366
+
367
+        return track[f]().catch(error => {
368
+            console.error(`set track ${f} failed`, error);
369
+
370
+            const setMuted
371
+                = track.mediaType === MEDIA_TYPE.AUDIO
372
+                    ? setAudioMuted
373
+                    : setVideoMuted;
374
+
375
+            dispatch(setMuted(!muted));
376
+        });
377
+    };
378
+}
379
+
347 380
 /**
348 381
  * Returns true if the provided JitsiTrack should be rendered as a mirror.
349 382
  *

+ 1
- 37
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, setAudioMuted, setVideoMuted } from '../media';
4
+import { MEDIA_TYPE } from '../media';
5 5
 
6 6
 const logger = require('jitsi-meet-logger').getLogger(__filename);
7 7
 
@@ -155,39 +155,3 @@ export function getTrackByJitsiTrack(tracks, jitsiTrack) {
155 155
 export function getTracksByMediaType(tracks, mediaType) {
156 156
     return tracks.filter(t => t.mediaType === mediaType);
157 157
 }
158
-
159
-/**
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
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.
166
- *
167
- * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
168
- * unmute.
169
- * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
170
- * <tt>true</tt>; otherwise, <tt>false</tt>.
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}
175
- */
176
-export function setTrackMuted(track, muted, { dispatch }) {
177
-    if (track.isMuted() === muted) {
178
-        return Promise.resolve();
179
-    }
180
-
181
-    const f = muted ? 'mute' : 'unmute';
182
-
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
-    });
193
-}

+ 12
- 13
react/features/base/tracks/middleware.js View File

@@ -13,8 +13,9 @@ import {
13 13
 } from '../media';
14 14
 import { MiddlewareRegistry } from '../redux';
15 15
 
16
+import { setTrackMuted } from './actions';
16 17
 import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
17
-import { getLocalTrack, setTrackMuted } from './functions';
18
+import { getLocalTrack } from './functions';
18 19
 
19 20
 declare var APP: Object;
20 21
 
@@ -23,7 +24,7 @@ declare var APP: Object;
23 24
  * respectively, creates/destroys local media tracks. Also listens to
24 25
  * media-related actions and performs corresponding operations with tracks.
25 26
  *
26
- * @param {Store} store - Redux store.
27
+ * @param {Store} store - The redux store.
27 28
  * @returns {Function}
28 29
  */
29 30
 MiddlewareRegistry.register(store => next => action => {
@@ -132,9 +133,9 @@ MiddlewareRegistry.register(store => next => action => {
132 133
 
133 134
 /**
134 135
  * Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
135
- * specific Redux store.
136
+ * specific redux store.
136 137
  *
137
- * @param {Store} store - The Redux store from which the local track associated
138
+ * @param {Store} store - The redux store from which the local track associated
138 139
  * with the specified <tt>mediaType</tt> is to be retrieved.
139 140
  * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
140 141
  * be retrieved from the specified <tt>store</tt>.
@@ -149,20 +150,18 @@ function _getLocalTrack(store, mediaType: MEDIA_TYPE) {
149 150
 /**
150 151
  * Mutes or unmutes a local track with a specific media type.
151 152
  *
152
- * @param {Store} store - The Redux store in which the specified action is
153
+ * @param {Store} store - The redux store in which the specified action is
153 154
  * dispatched.
154
- * @param {Action} action - The Redux action dispatched in the specified store.
155
+ * @param {Action} action - The redux action dispatched in the specified store.
155 156
  * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
156 157
  * which is being muted or unmuted.
157 158
  * @private
158 159
  * @returns {void}
159 160
  */
160
-function _setMuted(store, action, mediaType: MEDIA_TYPE) {
161
+function _setMuted(store, { muted }, mediaType: MEDIA_TYPE) {
161 162
     const localTrack = _getLocalTrack(store, mediaType);
162 163
 
163
-    if (localTrack) {
164
-        setTrackMuted(localTrack.jitsiTrack, action.muted, store);
165
-    }
164
+    localTrack && store.dispatch(setTrackMuted(localTrack.jitsiTrack, muted));
166 165
 }
167 166
 
168 167
 /**
@@ -170,11 +169,11 @@ function _setMuted(store, action, mediaType: MEDIA_TYPE) {
170 169
  * muted states of the local tracks of features/base/tracks with the muted
171 170
  * states of features/base/media.
172 171
  *
173
- * @param {Store} store - The Redux store in which the specified <tt>action</tt>
172
+ * @param {Store} store - The redux store in which the specified <tt>action</tt>
174 173
  * is being dispatched.
175
- * @param {Dispatch} next - The Redux dispatch function to dispatch the
174
+ * @param {Dispatch} next - The redux dispatch function to dispatch the
176 175
  * specified <tt>action</tt> to the specified <tt>store</tt>.
177
- * @param {Action} action - The Redux action <tt>TRACK_UPDATED</tt> which is
176
+ * @param {Action} action - The redux action <tt>TRACK_UPDATED</tt> which is
178 177
  * being dispatched in the specified <tt>store</tt>.
179 178
  * @private
180 179
  * @returns {Object} The new state that is the result of the reduction of the

Loading…
Cancel
Save